diff --git a/context/config_file.go b/context/config_file.go index 48f9ce482..31294eed9 100644 --- a/context/config_file.go +++ b/context/config_file.go @@ -6,7 +6,9 @@ import ( "io" "io/ioutil" "os" + "path" + "github.com/mitchellh/go-homedir" "gopkg.in/yaml.v3" ) @@ -16,6 +18,8 @@ type configEntry struct { } func parseConfigFile(fn string) (*configEntry, error) { + migrateConfigFile() + f, err := os.Open(fn) if err != nil { if errors.Is(err, os.ErrNotExist) { @@ -52,3 +56,44 @@ func parseConfig(r io.Reader) (*configEntry, error) { } return nil, fmt.Errorf("could not find config entry for %q", defaultHostname) } + +// This is a temporary function that will migrate the config file. It can be removed +// in January. +// +// If ~/.config/gh is a file, convert it to a directory and place the file +// into ~/.config/gh/config.yml +func migrateConfigFile() { + p, _ := homedir.Expand("~/.config/gh") + fi, err := os.Stat(p) + if err != nil { // This means the file doesn't exist, and that is fine. + return + } + if fi.Mode().IsDir() { + return + } + + content, err := ioutil.ReadFile(p) + if err != nil { + fmt.Fprintf(os.Stderr, "migration error: failed to read config at %s", p) + return + } + + err = os.Remove(p) + if err != nil { + fmt.Fprintf(os.Stderr, "migration error: failed to remove %s", p) + return + } + + err = os.MkdirAll(p, 0771) + if err != nil { + fmt.Fprintf(os.Stderr, "migration error: failed to mkdir %s", p) + return + } + + newPath := path.Join(p, "config.yml") + err = ioutil.WriteFile(newPath, []byte(content), 0771) + if err != nil { + fmt.Fprintf(os.Stderr, "migration error: failed write to new config path %s", newPath) + return + } +} diff --git a/main.go b/main.go index 6292fe004..e7f94a5d0 100644 --- a/main.go +++ b/main.go @@ -2,18 +2,13 @@ package main import ( "fmt" - "io/ioutil" "os" - "path" "strings" "github.com/github/gh-cli/command" - "github.com/mitchellh/go-homedir" ) func main() { - migrateConfig() - if cmd, err := command.RootCmd.ExecuteC(); err != nil { fmt.Fprintln(os.Stderr, err) _, isFlagError := err.(command.FlagError) @@ -23,44 +18,3 @@ func main() { os.Exit(1) } } - -// This is a temporary function that will migrate the config file. It can be removed -// in January. -// -// If ~/.config/gh is a file, convert it to a directory and place the file -// into ~/.config/gh/config.yml -func migrateConfig() { - p, _ := homedir.Expand("~/.config/gh") - fi, err := os.Stat(p) - if err != nil { // This means the file doesn't exist, and that is fine. - return - } - if fi.Mode().IsDir() { - return - } - - content, err := ioutil.ReadFile(p) - if err != nil { - fmt.Fprintf(os.Stderr, "migration error: failed to read config at %s", p) - return - } - - err = os.Remove(p) - if err != nil { - fmt.Fprintf(os.Stderr, "migration error: failed to remove %s", p) - return - } - - err = os.MkdirAll(p, 0771) - if err != nil { - fmt.Fprintf(os.Stderr, "migration error: failed to mkdir %s", p) - return - } - - newPath := path.Join(p, "config.yml") - err = ioutil.WriteFile(newPath, []byte(content), 0771) - if err != nil { - fmt.Fprintf(os.Stderr, "migration error: failed write to new config path %s", newPath) - return - } -}