Move to context package

This commit is contained in:
Corey Johnson 2019-12-04 15:39:25 -08:00
parent 14561cba9f
commit ae68acb901
2 changed files with 45 additions and 46 deletions

View file

@ -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
}
}

46
main.go
View file

@ -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
}
}