Skip to content

Commit af7b7a5

Browse files
authored
Add --dir option (#23)
* Update dependencies * Little refactoring to support plug-ins * Add --dir option * Close #20 * GH Actions updates
1 parent e5f0cf6 commit af7b7a5

File tree

17 files changed

+319
-191
lines changed

17 files changed

+319
-191
lines changed

.github/workflows/release.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ name: Create Release
88
jobs:
99
build:
1010
name: Build
11-
runs-on: ubuntu-20.04
11+
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
1414
os: [linux, darwin, windows]
@@ -22,11 +22,11 @@ jobs:
2222
- name: Set RELEASE_VERSION
2323
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
2424
- name: Setup Go
25-
uses: actions/setup-go@v1
25+
uses: actions/setup-go@v3
2626
with:
2727
go-version: '1.18'
2828
- name: Checkout Code
29-
uses: actions/checkout@v2
29+
uses: actions/checkout@v3
3030
- name: Add Windows Exe File Extension
3131
if: matrix.os == 'windows'
3232
run: |
@@ -35,9 +35,9 @@ jobs:
3535
env:
3636
GOOS: ${{ matrix.os }}
3737
GOARCH: ${{ matrix.arch }}
38-
run: go build -ldflags="-X 'main.Version=${{ env.RELEASE_VERSION }}'" -o build/terraform-backend-git-${{ matrix.os }}-${{ matrix.arch }}${{ env.FILE_EXTENSION }}
38+
run: go build -ldflags="-X 'github.com/plumber-cd/terraform-backend-git/cmd.Version=${{ env.RELEASE_VERSION }}'" -o build/terraform-backend-git-${{ matrix.os }}-${{ matrix.arch }}${{ env.FILE_EXTENSION }}
3939
- name: Upload Artifact
40-
uses: actions/upload-artifact@v1
40+
uses: actions/upload-artifact@v3
4141
with:
4242
name: terraform-backend-git
4343
path: build/terraform-backend-git-${{ matrix.os }}-${{ matrix.arch }}${{ env.FILE_EXTENSION }}
@@ -47,11 +47,12 @@ jobs:
4747
needs: build
4848
steps:
4949
- name: Checkout Code
50-
uses: actions/checkout@v2
50+
uses: actions/checkout@v3
5151
- name: Download Artifacts
52-
uses: actions/download-artifact@v1
52+
uses: actions/download-artifact@v3
5353
with:
5454
name: terraform-backend-git
55+
path: terraform-backend-git
5556
- name: Generate SHA256SUMS
5657
run: |
5758
cd terraform-backend-git && sha256sum * > SHA256SUMS

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.0.19] - 2022-05-14
10+
11+
### Changed
12+
13+
- Introduced `--dir` option under `git` backend - now current working directory can be changed dynamically
14+
915
## [0.0.18] - 2022-04-30
1016

1117
### Changed

cmd/discovery/discovery.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Package discovery holds information about discovered cmd
2+
package discovery
3+
4+
import (
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var root *cobra.Command
9+
10+
// backends is a list of backend types available via cmd wrapper (such as 'git')
11+
var backends = make([]*cobra.Command, 0)
12+
13+
// wrappers is a list of wrapper commands to add as child to each backend from above
14+
var wrappers = make([]*cobra.Command, 0)
15+
16+
func RegisterRoot(r *cobra.Command) {
17+
root = r
18+
19+
for _, backend := range backends {
20+
root.AddCommand(backend)
21+
}
22+
}
23+
24+
func Root() *cobra.Command {
25+
return root
26+
}
27+
28+
func RegisterBackend(backend *cobra.Command) {
29+
backends = append(backends, backend)
30+
31+
for _, wrapper := range wrappers {
32+
backend.AddCommand(wrapper)
33+
}
34+
35+
if root != nil {
36+
root.AddCommand(backend)
37+
}
38+
}
39+
40+
func RegisterWrapper(wrapper *cobra.Command) {
41+
wrappers = append(wrappers, wrapper)
42+
43+
for _, backend := range backends {
44+
backend.AddCommand(wrapper)
45+
}
46+
}

git_backend_wrappers.go renamed to cmd/git_backend.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package cmd
22

33
import (
44
"log"
@@ -9,7 +9,10 @@ import (
99
"github.com/spf13/cobra"
1010
"github.com/spf13/viper"
1111

12+
"github.com/plumber-cd/terraform-backend-git/cmd/discovery"
1213
"github.com/plumber-cd/terraform-backend-git/server"
14+
15+
_ "github.com/plumber-cd/terraform-backend-git/storages/git" // force it to init
1316
)
1417

1518
// gitHTTPBackendConfigPath is a path to the backend tf config to generate
@@ -21,6 +24,13 @@ var gitBackendCmd = &cobra.Command{
2124
Short: "Start backend in Git storage mode and execute the wrapper",
2225
Long: "It will also generate " + gitHTTPBackendConfigPath + " in current working directory pointing to this backend",
2326
PersistentPreRun: func(cmd *cobra.Command, args []string) {
27+
cd := viper.GetString("git.dir")
28+
if cd != "" {
29+
if err := os.Chdir(cd); err != nil {
30+
log.Fatal(err)
31+
}
32+
}
33+
2434
t, err := template.New(gitHTTPBackendConfigPath).Parse(`
2535
terraform {
2636
backend "http" {
@@ -70,3 +80,20 @@ terraform {
7080
}
7181
},
7282
}
83+
84+
func init() {
85+
gitBackendCmd.PersistentFlags().StringP("repository", "r", "", "Repository to use as storage")
86+
viper.BindPFlag("git.repository", gitBackendCmd.PersistentFlags().Lookup("repository"))
87+
88+
gitBackendCmd.PersistentFlags().StringP("ref", "b", "master", "Ref (branch) to use")
89+
viper.BindPFlag("git.ref", gitBackendCmd.PersistentFlags().Lookup("ref"))
90+
viper.SetDefault("git.ref", "master")
91+
92+
gitBackendCmd.PersistentFlags().StringP("state", "s", "", "Ref (branch) to use")
93+
viper.BindPFlag("git.state", gitBackendCmd.PersistentFlags().Lookup("state"))
94+
95+
gitBackendCmd.PersistentFlags().StringP("dir", "d", "", "Change current working directory")
96+
viper.BindPFlag("git.dir", gitBackendCmd.PersistentFlags().Lookup("dir"))
97+
98+
discovery.RegisterBackend(gitBackendCmd)
99+
}

cmd/root.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"os/exec"
8+
"strings"
9+
10+
"github.com/mitchellh/go-homedir"
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
13+
14+
"github.com/plumber-cd/terraform-backend-git/cmd/discovery"
15+
"github.com/plumber-cd/terraform-backend-git/pid"
16+
"github.com/plumber-cd/terraform-backend-git/server"
17+
)
18+
19+
var cfgFile string
20+
21+
// rootCmd main command that just starts the server and keeps listening on port until terminated
22+
var rootCmd = &cobra.Command{
23+
Use: "terraform-backend-git",
24+
Short: "Terraform HTTP backend implementation that uses Git as storage",
25+
// will use known storage types in this repository and start a local HTTP server
26+
Run: func(cmd *cobra.Command, args []string) {
27+
if err := pid.LockPidFile(); err != nil {
28+
log.Fatal(err)
29+
}
30+
31+
server.Start()
32+
},
33+
}
34+
35+
// BackendsCmds is a list of backend types available via cmd wrapper
36+
var BackendsCmds = make([]*cobra.Command, 0)
37+
38+
// WrappersCmds is a list of wrapper commands available to run wrapped into a backend wrapper
39+
// i.e. backand wrapper "git" will start an http backend in Git storage mode
40+
// and "terraform" wrapper started from it will use terraform while that backend is running
41+
var WrappersCmds = make([]*cobra.Command, 0)
42+
43+
func Exec() {
44+
if err := discovery.Root().Execute(); err != nil {
45+
// If the error was coming from a wrapper, must respect it's exit code
46+
exitErr, ok := err.(*exec.ExitError)
47+
if ok {
48+
os.Exit(exitErr.ExitCode())
49+
}
50+
51+
fmt.Println(err)
52+
os.Exit(1)
53+
}
54+
}
55+
56+
func init() {
57+
// keep the output clean as in wrapper mode it'll mess out with Terraform own output
58+
log.SetFlags(0)
59+
log.SetPrefix("[terraform-backend-git]: ")
60+
61+
cobra.OnInitialize(initConfig)
62+
63+
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is terraform-backend-git.hcl)")
64+
65+
rootCmd.PersistentFlags().StringP("address", "a", "127.0.0.1:6061", "Specify the listen address")
66+
viper.BindPFlag("address", rootCmd.PersistentFlags().Lookup("address"))
67+
viper.SetDefault("address", "127.0.0.1:6061")
68+
rootCmd.PersistentFlags().BoolP("access-logs", "l", false, "Log HTTP requests to the console")
69+
viper.BindPFlag("accessLogs", rootCmd.PersistentFlags().Lookup("access-logs"))
70+
viper.SetDefault("accessLogs", false)
71+
72+
discovery.RegisterRoot(rootCmd)
73+
}
74+
75+
func initConfig() {
76+
viper.SetConfigType("hcl")
77+
viper.SetConfigName("terraform-backend-git")
78+
79+
if cfgFile != "" {
80+
viper.SetConfigFile(cfgFile)
81+
} else {
82+
home, err := homedir.Dir()
83+
if err != nil {
84+
log.Fatal(err)
85+
}
86+
viper.AddConfigPath(home)
87+
88+
cwd, err := os.Getwd()
89+
if err != nil {
90+
log.Fatal(err)
91+
}
92+
viper.AddConfigPath(cwd)
93+
}
94+
95+
viper.AutomaticEnv()
96+
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
97+
viper.SetEnvPrefix("TF_BACKEND_GIT")
98+
99+
if err := viper.ReadInConfig(); err == nil {
100+
log.Println("Using config file:", viper.ConfigFileUsed())
101+
}
102+
}

cmd/stop.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cmd
2+
3+
import (
4+
"log"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/plumber-cd/terraform-backend-git/pid"
9+
)
10+
11+
// stopCmd will stop the server started via rootCmd via it's pid file
12+
var stopCmd = &cobra.Command{
13+
Use: "stop",
14+
Short: "Stop the currently running backend",
15+
Run: func(cmd *cobra.Command, args []string) {
16+
if err := pid.StopPidFile(); err != nil {
17+
log.Fatal(err)
18+
}
19+
},
20+
}
21+
22+
func init() {
23+
rootCmd.AddCommand(stopCmd)
24+
}

tf_backend_wrapper.go renamed to cmd/tf_wrapper.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package main
1+
package cmd
22

33
import (
44
"os"
55
"os/exec"
66

77
"github.com/spf13/cobra"
88
"github.com/spf13/viper"
9+
10+
"github.com/plumber-cd/terraform-backend-git/cmd/discovery"
911
)
1012

1113
// terraformWrapperCmd will pass all arguments to terraform,
@@ -31,3 +33,13 @@ var terraformWrapperCmd = &cobra.Command{
3133
return nil
3234
},
3335
}
36+
37+
func init() {
38+
terraformWrapperCmd.Flags().StringP("tf", "t", "terraform", "Path to terraform binary")
39+
viper.BindPFlag("wrapper.tf.bin", terraformWrapperCmd.Flags().Lookup("tf"))
40+
viper.SetDefault("wrapper.tf.bin", "terraform")
41+
42+
terraformWrapperCmd.Flags().SetInterspersed(false)
43+
44+
discovery.RegisterWrapper(terraformWrapperCmd)
45+
}

cmd/version.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// Version holds the version binary built with - must be injected from build process via -ldflags="-X 'github.com/plumber-cd/terraform-backend-git/cmd.Version=${{ env.RELEASE_VERSION }}'"
10+
var Version = "dev"
11+
12+
// versionCmd will print the version
13+
var versionCmd = &cobra.Command{
14+
Use: "version",
15+
Short: "Print version",
16+
Run: func(cmd *cobra.Command, args []string) {
17+
fmt.Println(Version)
18+
},
19+
}
20+
21+
func init() {
22+
rootCmd.AddCommand(versionCmd)
23+
}

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ require (
1010
github.com/spf13/cobra v1.4.0
1111
github.com/spf13/viper v1.11.0
1212
github.com/xanzy/ssh-agent v0.3.1
13-
golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f
14-
golang.org/x/sys v0.0.0-20220429233432-b5fbb4746d32
13+
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9
14+
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a
1515
)
1616

1717
require (
1818
github.com/Microsoft/go-winio v0.5.2 // indirect
19-
github.com/ProtonMail/go-crypto v0.0.0-20220407094043-a94812496cf5 // indirect
19+
github.com/ProtonMail/go-crypto v0.0.0-20220512085406-902f79d34c9f // indirect
2020
github.com/acomagu/bufpipe v1.0.3 // indirect
2121
github.com/emirpasic/gods v1.18.1 // indirect
22-
github.com/felixge/httpsnoop v1.0.2 // indirect
22+
github.com/felixge/httpsnoop v1.0.3 // indirect
2323
github.com/fsnotify/fsnotify v1.5.4 // indirect
2424
github.com/go-git/gcfg v1.5.0 // indirect
2525
github.com/hashicorp/hcl v1.0.0 // indirect
@@ -30,17 +30,17 @@ require (
3030
github.com/magiconair/properties v1.8.6 // indirect
3131
github.com/mitchellh/mapstructure v1.5.0 // indirect
3232
github.com/pelletier/go-toml v1.9.5 // indirect
33-
github.com/pelletier/go-toml/v2 v2.0.0 // indirect
33+
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
3434
github.com/sergi/go-diff v1.2.0 // indirect
3535
github.com/spf13/afero v1.8.2 // indirect
36-
github.com/spf13/cast v1.4.1 // indirect
36+
github.com/spf13/cast v1.5.0 // indirect
3737
github.com/spf13/jwalterweatherman v1.1.0 // indirect
3838
github.com/spf13/pflag v1.0.5 // indirect
3939
github.com/subosito/gotenv v1.2.0 // indirect
40-
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
40+
golang.org/x/net v0.0.0-20220513224357-95641704303c // indirect
4141
golang.org/x/text v0.3.7 // indirect
4242
gopkg.in/ini.v1 v1.66.4 // indirect
4343
gopkg.in/warnings.v0 v0.1.2 // indirect
4444
gopkg.in/yaml.v2 v2.4.0 // indirect
45-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
45+
gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99 // indirect
4646
)

0 commit comments

Comments
 (0)