diff --git a/cmd/spr/main.go b/cmd/spr/main.go index f857d94..13781d1 100644 --- a/cmd/spr/main.go +++ b/cmd/spr/main.go @@ -40,6 +40,12 @@ func main() { } cfg := config_parser.ParseConfig(gitcmd) + + err = config_parser.CheckConfig(cfg) + if err != nil { + fmt.Println(err) + os.Exit(2) + } gitcmd = realgit.NewGitCmd(cfg) ctx := context.Background() diff --git a/config/config_parser/config_parser.go b/config/config_parser/config_parser.go index 59268ae..37150f0 100644 --- a/config/config_parser/config_parser.go +++ b/config/config_parser/config_parser.go @@ -1,10 +1,12 @@ package config_parser import ( + "errors" "fmt" "os" "path" "path/filepath" + "strings" "github.com/ejoffe/rake" "github.com/ejoffe/spr/config" @@ -49,9 +51,26 @@ func ParseConfig(gitcmd git.GitInterface) *config.Config { rake.LoadSources(cfg.State, rake.YamlFileWriter(InternalConfigFilePath())) + // init case : if yaml config files not found : create them + if _, err := os.Stat(RepoConfigFilePath(gitcmd)); errors.Is(err, os.ErrNotExist) { + rake.LoadSources(cfg.Repo, + rake.YamlFileWriter(RepoConfigFilePath(gitcmd))) + } + + if _, err := os.Stat(UserConfigFilePath()); errors.Is(err, os.ErrNotExist) { + rake.LoadSources(cfg.User, + rake.YamlFileWriter(UserConfigFilePath())) + } return cfg } +func CheckConfig(cfg *config.Config) error { + if strings.Contains(cfg.Repo.GitHubBranch, "/") { + return errors.New("Remote branch name must not contain backslashes '/'") + } + return nil +} + func RepoConfigFilePath(gitcmd git.GitInterface) string { rootdir := gitcmd.RootDir() filepath := filepath.Clean(path.Join(rootdir, ".spr.yml")) diff --git a/github/githubclient/star.go b/github/githubclient/star.go index ce19bf4..b7a0585 100644 --- a/github/githubclient/star.go +++ b/github/githubclient/star.go @@ -22,7 +22,22 @@ const ( func (c *client) MaybeStar(ctx context.Context, cfg *config.Config) { if !cfg.State.Stargazer && cfg.State.RunCount%promptCycle == 0 { - if c.isStar(ctx) { + starred, err := c.isStar(ctx) + if err != nil { + fmt.Println("enjoying git spr? [Y/n]") + fmt.Print(" please add a star at https://github.com/ejoffe/spr") + reader := bufio.NewReader(os.Stdin) + line, _ := reader.ReadString('\n') + line = strings.TrimSpace(line) + if line != "n" { + cfg.State.Stargazer = true + rake.LoadSources(cfg.State, + rake.YamlFileWriter(config_parser.InternalConfigFilePath())) + fmt.Println("Thank You! Happy Coding!") + } + } + + if starred { log.Debug().Bool("stargazer", true).Msg("MaybeStar") cfg.State.Stargazer = true rake.LoadSources(cfg.State, @@ -39,30 +54,32 @@ func (c *client) MaybeStar(ctx context.Context, cfg *config.Config) { cfg.State.Stargazer = true rake.LoadSources(cfg.State, rake.YamlFileWriter(config_parser.InternalConfigFilePath())) - fmt.Println("Thank you! Happy Coding!") + fmt.Println("Thank You! Happy Coding!") } } } } -func (c *client) isStar(ctx context.Context) bool { +func (c *client) isStar(ctx context.Context) (bool, error) { iteration := 0 cursor := "" for { resp, err := c.api.StarCheck(ctx, &cursor) - check(err) + if err != nil { + return false, err + } edgeCount := len(*resp.Viewer.StarredRepositories.Edges) if edgeCount == 0 { log.Debug().Bool("stargazer", false).Msg("MaybeStar::isStar") - return false + return false, nil } sprRepo := fmt.Sprintf("%s/%s", sprRepoOwner, sprRepoName) for _, node := range *resp.Viewer.StarredRepositories.Nodes { if node.NameWithOwner == sprRepo { log.Debug().Bool("stargazer", true).Msg("MaybeStar::isStar") - return true + return true, nil } } @@ -73,7 +90,7 @@ func (c *client) isStar(ctx context.Context) bool { if iteration > 10 { // too many stars in the sky log.Debug().Bool("stargazer", false).Msg("MaybeStar::isStar (too many stars)") - return false + return false, nil } } }