Add a config migration function
This commit is contained in:
parent
4b9cca3129
commit
afaab6d16c
1 changed files with 44 additions and 0 deletions
44
main.go
44
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue