From afaab6d16c940fb20aa5af6c9370d1f24204abe8 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 3 Dec 2019 16:36:45 -0800 Subject: [PATCH] Add a config migration function --- main.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/main.go b/main.go index e7f94a5d0..a46804683 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,18 @@ 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) @@ -18,3 +23,42 @@ func main() { os.Exit(1) } } + +// 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 { + fmt.Fprintf(os.Stderr, "migration error: failed to stat %s", p) + 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 + } +}