Skip to content

Commit

Permalink
fix(config): fix default hostname when hosts.yml is empty (#203)
Browse files Browse the repository at this point in the history
Because

- when `hosts.yml` is empty, the `default_hostname` value in
`config.yml` should fallback to the default hostname `api.instill.tech`.
- the bug fails `inst auth login` when the user run `inst local deploy`
at the very beginning (with a fresh `inst` installed) followed by `inst
local undeploy` right away, where the `default_hostname` will be left
with `localhost:8080`.
- with a fresh `inst` installed, `inst auth login` uses the default
hostname `api.instill.tech`.

This commit

- fix the bug
  • Loading branch information
pinglin committed Oct 24, 2023
1 parent 141b571 commit a163ef2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/config/config_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Config interface {
Get(string, string) (string, error)
GetWithSource(string, string) (string, string, error)
Set(string, string, string) error
UnsetHost(string)
UnsetHost(string) error
Hosts() ([]string, error)
HostsTyped() ([]HostConfigTyped, error)
DefaultHostname() string
Expand Down
19 changes: 16 additions & 3 deletions internal/config/from_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,31 @@ func (c *fileConfig) Set(hostname, key, value string) error {
}
}

func (c *fileConfig) UnsetHost(hostname string) {
func (c *fileConfig) UnsetHost(hostname string) error {

if hostname == "" {
return
return nil
}

hostsEntry, err := c.FindEntry("hosts")
if err != nil {
return
return err
}

cm := ConfigMap{hostsEntry.ValueNode}
cm.RemoveEntry(hostname)

_, err = c.hostEntries()
if strings.Contains(err.Error(), "could not find any host configurations") {
// no hosts, fallback to the default hostname
defaultHost := instance.FallbackHostname()
err = c.Set("", "default_hostname", defaultHost)
if err != nil {
return err
}
}

return nil
}

func (c *fileConfig) ConfigForHost(hostname string) (*HostConfig, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (c ConfigStub) Hosts() ([]string, error) {
return nil, nil
}

func (c ConfigStub) UnsetHost(hostname string) {
// TODO
func (c ConfigStub) UnsetHost(hostname string) error {
return nil
}

func (c ConfigStub) CheckWriteable(host, key string) error {
Expand Down
6 changes: 5 additions & 1 deletion pkg/cmd/auth/logout/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ func logoutRun(opts *LogoutOptions) error {
}

// TODO invalidate the token instead of removing the whole instance
cfg.UnsetHost(hostname)
err = cfg.UnsetHost(hostname)
if err != nil {
return fmt.Errorf("error unset hostname '%s' - %w", hostname, err)
}

err = cfg.Write()
if err != nil {
return fmt.Errorf("failed to write config, authentication configuration not updated: %w", err)
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/instance/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ func RunRemove(opts *RemoveOptions) error {
return fmt.Errorf("ERROR: instance '%s' does not exists", apiHost)
}

opts.Config.UnsetHost(opts.APIHostname)
err = opts.Config.UnsetHost(opts.APIHostname)
if err != nil {
return fmt.Errorf("error unset hostname '%s' - %w", opts.APIHostname, err)
}
err = opts.Config.Write()
if err != nil {
return fmt.Errorf("error removing hostname '%s' - %w", opts.APIHostname, err)
Expand Down

0 comments on commit a163ef2

Please sign in to comment.