diff --git a/.gitignore b/.gitignore index 98b9e81..50598ae 100644 --- a/.gitignore +++ b/.gitignore @@ -30,5 +30,5 @@ go.work # End of https://www.toptal.com/developers/gitignore/api/go -dist safebox +dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..72146f0 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,27 @@ +before: + hooks: + - go mod tidy +builds: + - binary: safebox + env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + +checksum: + name_template: 'checksums.txt' + +snapshot: + name_template: "{{ incpatch .Version }}-next" + +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + +# yaml-language-server: $schema=https://goreleaser.com/static/schema.json +# vim: set ts=2 sw=2 tw=0 fo=cnqoj diff --git a/Makefile b/Makefile deleted file mode 100644 index 1172e69..0000000 --- a/Makefile +++ /dev/null @@ -1,48 +0,0 @@ -# TODO: remove hard coded version -VERSION := 1 -ifndef VERSION - VERSION := $(shell git describe --tags --always --dirty="-dev") -endif - -ifndef TARGETARCH - TARGETARCH := $(shell arch) -endif - -LDFLAGS := -ldflags='-X "main.Version=$(VERSION)"' - -test: - go test -v ./... - -all: darwin-amd64 darwin-arm64 linux-amd64 windows-amd64.exe - -clean: | - rm -rf ./dist - rm -f ./safebox - -dist/: - mkdir -p dist - -build: clean safebox - -safebox: - CGO_ENABLED=0 go build -trimpath $(LDFLAGS) -o $@ - -linux: linux-$(TARGETARCH) - cp $^ safebox - -darwin-amd64: | dist/ - GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -trimpath $(LDFLAGS) -o dist/safebox-$(VERSION)-$@ - -darwin-arm64: | dist/ - GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -trimpath $(LDFLAGS) -o dist/safebox-$(VERSION)-$@ - -linux-amd64: | dist/ - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath $(LDFLAGS) -o dist/safebox-$(VERSION)-$@ - -linux-arm64 linux-aarch64: | dist/ - GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -trimpath $(LDFLAGS) -o dist/safebox-$(VERSION)-$@ - -windows-amd64.exe: | dist/ - GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -trimpath $(LDFLAGS) -o dist/safebox-$(VERSION)-$@ - -.PHONY: clean all linux diff --git a/cmd/list.go b/cmd/list.go index 02ae103..f9840f0 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -32,7 +32,13 @@ func init() { } func list(cmd *cobra.Command, args []string) error { - store, err := getStore() + config, err := loadConfig() + + if err != nil { + return errors.Wrap(err, "failed to load config") + } + + store, err := store.GetStore(config.Provider) if err != nil { return errors.Wrap(err, "failed to instantiate store") @@ -40,7 +46,7 @@ func list(cmd *cobra.Command, args []string) error { var keys []string - for _, value := range Config.Configs { + for _, value := range config.Configs { keys = append(keys, value.Key) } diff --git a/cmd/root.go b/cmd/root.go index 03882ed..11038b3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,38 +1,34 @@ package cmd import ( - "fmt" "os" "strings" c "github.com/adikari/safebox/v2/config" - "github.com/adikari/safebox/v2/store" - "github.com/pkg/errors" "github.com/spf13/cobra" ) var ( stage string pathToConfig string - Config *c.Config TimeFormat = "2006-01-02 15:04:05" ) var rootCmd = &cobra.Command{ - Use: "safebox", - Short: "SafeBox is a secret manager CLI program", - Long: `A Fast and Flexible secret manager built with love by adikari in Go.`, - PersistentPreRunE: prerun, - SilenceUsage: true, + Use: "safebox", + Short: "SafeBox is a secret manager CLI program", + Long: `A Fast and Flexible secret manager built with love by adikari in Go.`, + SilenceUsage: true, Run: func(cmd *cobra.Command, args []string) { cmd.Usage() }, } func init() { - rootCmd.PersistentFlags().StringVarP(&stage, "stage", "s", "", "stage to deploy to") + rootCmd.PersistentFlags().StringVarP(&stage, "stage", "s", "dev", "stage to deploy to") + rootCmd.PersistentFlags().StringVarP(&pathToConfig, "config", "c", "safebox.yml", "path to safebox configuration file") - rootCmd.MarkPersistentFlagRequired("stage") + rootCmd.MarkFlagFilename("config") } func Execute(version string) { @@ -47,17 +43,6 @@ func Execute(version string) { } } -func prerun(cmd *cobra.Command, args []string) error { - c, err := loadConfig() - - if err != nil { - return errors.Wrap(err, "failed to load config") - } - - Config = c - return nil -} - func loadConfig() (*c.Config, error) { params := c.LoadParam{ Path: pathToConfig, @@ -66,12 +51,3 @@ func loadConfig() (*c.Config, error) { return c.Load(params) } - -func getStore() (store.Store, error) { - switch Config.Provider { - case "ssm": - return store.NewSSMStore() - default: - return nil, fmt.Errorf("invalid provider `%s`", Config.Provider) - } -} diff --git a/cmd/run.go b/cmd/run.go index a61e5ff..354b1c2 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/adikari/safebox/v2/store" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -18,13 +19,19 @@ func init() { } func run(cmd *cobra.Command, args []string) error { - store, err := getStore() + config, err := loadConfig() + + if err != nil { + return errors.Wrap(err, "failed to load config") + } + + store, err := store.GetStore(config.Provider) if err != nil { return errors.Wrap(err, "failed to instantiate store") } - err = store.PutMany(Config.Configs) + err = store.PutMany(config.Configs) if err != nil { return errors.Wrap(err, "failed to write param") diff --git a/config/config.go b/config/config.go index e2c249c..b3714ff 100644 --- a/config/config.go +++ b/config/config.go @@ -41,9 +41,18 @@ func Load(param LoadParam) (*Config, error) { return nil, fmt.Errorf("could not parse safebox config file %s", param.Path) } + if rc.Service == "" { + return nil, fmt.Errorf("'service' missing in config file%s", param.Path) + } + c := Config{} c.Service = rc.Service c.Provider = rc.Provider + + if c.Provider == "" { + c.Provider = store.SsmProvider + } + parseConfig(rc, &c, param) return &c, nil diff --git a/store/store.go b/store/store.go index f6a36e1..9a0b993 100644 --- a/store/store.go +++ b/store/store.go @@ -2,6 +2,8 @@ package store import ( "errors" + "fmt" + "strings" "time" ) @@ -14,6 +16,20 @@ type Config struct { DataType string } +func (c *Config) Key() string { + parts := strings.Split(*c.Name, "/") + return parts[len(parts)-1] +} + +func (c *Config) Path() string { + parts := strings.Split(*c.Name, "/") + return strings.Join(parts[0:len(parts)-1], "/") +} + +const ( + SsmProvider = "ssm" +) + type ConfigInput struct { Key string Value string @@ -32,3 +48,12 @@ type Store interface { GetAll() ([]Config, error) Delete(key string) error } + +func GetStore(provider string) (Store, error) { + switch provider { + case SsmProvider: + return NewSSMStore() + default: + return nil, fmt.Errorf("invalid provider `%s`", provider) + } +}