From 8beafba259297941617768d1501669d7c854df2e Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Wed, 12 Jul 2023 11:01:32 +0700 Subject: [PATCH] fix: Bug when writing config file if directory doesn't exist + Add arm64 Docker build (#55) * build: Add goreleaser config to build Docker image for arm64 architecture * fix: Use https for default urls * fix: Ensure config directory exists before writing * chore: Rename config methods * chore: Increment package.json version --- .goreleaser/linux.yml | 22 +++++++++++++++++++-- package.json | 2 +- pkg/config/config.go | 44 ++++++++++++++++++++++++++++-------------- pkg/config/profile.go | 10 +++++----- pkg/hookdeck/client.go | 4 ++-- 5 files changed, 58 insertions(+), 24 deletions(-) diff --git a/.goreleaser/linux.yml b/.goreleaser/linux.yml index 8a6d1c7..7cfbcdf 100644 --- a/.goreleaser/linux.yml +++ b/.goreleaser/linux.yml @@ -67,8 +67,8 @@ dockers: - hookdeck - hookdeck-linux image_templates: - - "hookdeck/hookdeck-cli:latest" - - "hookdeck/hookdeck-cli:{{ .Tag }}" + - "hookdeck/hookdeck-cli:latest-amd64" + - "hookdeck/hookdeck-cli:{{ .Tag }}-amd64" build_flag_templates: - "--pull" - "--label=org.opencontainers.image.created={{.Date}}" @@ -77,3 +77,21 @@ dockers: - "--label=org.opencontainers.image.version={{.Version}}" - "--label=repository=https://github.com/hookdeck/hookdeck-cli" - "--label=homepage=https://hookdeck.com" + - "--platform=linux/amd64" + - goos: linux + goarch: arm64 + ids: + - hookdeck + - hookdeck-linux-arm + image_templates: + - "hookdeck/hookdeck-cli:latest-arm64" + - "hookdeck/hookdeck-cli:{{ .Tag }}-arm64" + build_flag_templates: + - "--pull" + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.name={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=repository=https://github.com/hookdeck/hookdeck-cli" + - "--label=homepage=https://hookdeck.com" + - "--platform=linux/arm64/v8" diff --git a/package.json b/package.json index d226d34..0c7f6ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hookdeck-cli", - "version": "0.8.0", + "version": "0.8.1", "description": "Hookdeck CLI", "repository": { "type": "git", diff --git a/pkg/config/config.go b/pkg/config/config.go index 8290d4a..1b0c362 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -87,8 +87,8 @@ func (c *Config) InitConfig() { c.LocalConfig = viper.New() // Read global config - GlobalConfigFolder := c.GetConfigFolder(os.Getenv("XDG_CONFIG_HOME")) - c.GlobalConfigFile = filepath.Join(GlobalConfigFolder, "config.toml") + globalConfigFolder := c.GetConfigFolder(os.Getenv("XDG_CONFIG_HOME")) + c.GlobalConfigFile = filepath.Join(globalConfigFolder, "config.toml") c.GlobalConfig.SetConfigType("toml") c.GlobalConfig.SetConfigFile(c.GlobalConfigFile) c.GlobalConfig.SetConfigPermissions(os.FileMode(0600)) @@ -110,19 +110,19 @@ func (c *Config) InitConfig() { if err != nil { log.Fatal(err) } - LocalConfigFile := "" + localConfigFile := "" if c.LocalConfigFile == "" { - LocalConfigFile = filepath.Join(workspaceFolder, ".hookdeck/config.toml") + localConfigFile = filepath.Join(workspaceFolder, ".hookdeck/config.toml") } else { if filepath.IsAbs(c.LocalConfigFile) { - LocalConfigFile = c.LocalConfigFile + localConfigFile = c.LocalConfigFile } else { - LocalConfigFile = filepath.Join(workspaceFolder, c.LocalConfigFile) + localConfigFile = filepath.Join(workspaceFolder, c.LocalConfigFile) } } c.LocalConfig.SetConfigType("toml") - c.LocalConfig.SetConfigFile(LocalConfigFile) - c.LocalConfigFile = LocalConfigFile + c.LocalConfig.SetConfigFile(localConfigFile) + c.LocalConfigFile = localConfigFile if err := c.LocalConfig.ReadInConfig(); err == nil { log.WithFields(log.Fields{ "prefix": "config.Config.InitConfig", @@ -243,18 +243,21 @@ func (c *Config) RemoveAllProfiles() error { runtimeViper.SetConfigType("toml") runtimeViper.SetConfigFile(c.GlobalConfig.ConfigFileUsed()) c.GlobalConfig = runtimeViper - return c.GlobalConfig.WriteConfig() + return c.WriteGlobalConfig() } -func (c *Config) SaveLocalConfig() error { - if err := ensureDirectoy(filepath.Dir(c.LocalConfigFile)); err != nil { +func (c *Config) WriteGlobalConfig() error { + if err := makePath(c.GlobalConfig.ConfigFileUsed()); err != nil { return err } - return c.LocalConfig.WriteConfig() + return c.GlobalConfig.WriteConfig() } -func ensureDirectoy(path string) error { - return os.MkdirAll(path, os.ModePerm) +func (c *Config) WriteLocalConfig() error { + if err := makePath(c.LocalConfig.ConfigFileUsed()); err != nil { + return err + } + return c.LocalConfig.WriteConfig() } // Construct the config struct from flags > local config > global config @@ -314,6 +317,19 @@ func removeKey(v *viper.Viper, key string) (*viper.Viper, error) { return nv, nil } +func makePath(path string) error { + dir := filepath.Dir(path) + + if _, err := os.Stat(dir); os.IsNotExist(err) { + err = os.MkdirAll(dir, os.ModePerm) + if err != nil { + return err + } + } + + return nil +} + // taken from https://github.com/spf13/viper/blob/master/util.go#L199, // we need this to delete configs, remove when viper supprts unset natively func deepSearch(m map[string]interface{}, path []string) map[string]interface{} { diff --git a/pkg/config/profile.go b/pkg/config/profile.go index f9afe91..d44782d 100644 --- a/pkg/config/profile.go +++ b/pkg/config/profile.go @@ -22,16 +22,16 @@ func (p *Profile) SaveProfile(local bool) error { // and we don't need to expose it to the end user if local { p.Config.GlobalConfig.Set(p.GetConfigField("api_key"), p.APIKey) - if err := p.Config.GlobalConfig.WriteConfig(); err != nil { + if err := p.Config.WriteGlobalConfig(); err != nil { return err } p.Config.LocalConfig.Set("workspace_id", p.TeamID) - return p.Config.SaveLocalConfig() + return p.Config.WriteLocalConfig() } else { p.Config.GlobalConfig.Set(p.GetConfigField("api_key"), p.APIKey) p.Config.GlobalConfig.Set(p.GetConfigField("workspace_id"), p.TeamID) p.Config.GlobalConfig.Set(p.GetConfigField("workspace_mode"), p.TeamMode) - return p.Config.GlobalConfig.WriteConfig() + return p.Config.WriteGlobalConfig() } } @@ -51,12 +51,12 @@ func (p *Profile) RemoveProfile() error { runtimeViper.SetConfigType("toml") runtimeViper.SetConfigFile(p.Config.GlobalConfig.ConfigFileUsed()) p.Config.GlobalConfig = runtimeViper - return p.Config.GlobalConfig.WriteConfig() + return p.Config.WriteGlobalConfig() } func (p *Profile) UseProfile() error { p.Config.GlobalConfig.Set("profile", p.Name) - return p.Config.GlobalConfig.WriteConfig() + return p.Config.WriteGlobalConfig() } func (p *Profile) ValidateAPIKey() error { diff --git a/pkg/hookdeck/client.go b/pkg/hookdeck/client.go index 2cccb26..679966e 100644 --- a/pkg/hookdeck/client.go +++ b/pkg/hookdeck/client.go @@ -22,9 +22,9 @@ const DefaultAPIBaseURL = "https://api.hookdeck.com" const DefaultDashboardURL = "https://dashboard.hookdeck.com" // DefaultDashboardBaseURL is the default base URL for dashboard requests -const DefaultDashboardBaseURL = "http://dashboard.hookdeck.com" +const DefaultDashboardBaseURL = "https://dashboard.hookdeck.com" -const DefaultConsoleBaseURL = "http://console.hookdeck.com" +const DefaultConsoleBaseURL = "https://console.hookdeck.com" const DefaultWebsocektURL = "wss://ws.hookdeck.com"