Skip to content

Commit

Permalink
fix: Bug when writing config file if directory doesn't exist + Add ar…
Browse files Browse the repository at this point in the history
…m64 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
  • Loading branch information
alexluong authored Jul 12, 2023
1 parent bcebadb commit 8beafba
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
22 changes: 20 additions & 2 deletions .goreleaser/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"
Expand All @@ -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"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hookdeck-cli",
"version": "0.8.0",
"version": "0.8.1",
"description": "Hookdeck CLI",
"repository": {
"type": "git",
Expand Down
44 changes: 30 additions & 14 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{} {
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/hookdeck/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 8beafba

Please sign in to comment.