Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
Error handling, unused params, dot comments, naming
  • Loading branch information
LoyalPotato committed Nov 18, 2023
1 parent 13b7edc commit ef134a3
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 43 deletions.
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func main() {

if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func rootCmd() cobra.Command {
DisableAutoGenTag: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if err := git.IsGitRepo(); err != nil {
fmt.Println(messages.NotGitEnv)
fmt.Println(messages.Not_Git_Env)
os.Exit(1)
}

if !gittown.IsInstalled() {
fmt.Println(messages.NoGitTown)
fmt.Println(messages.No_Git_Town)
os.Exit(1)
}
},
Expand Down
14 changes: 11 additions & 3 deletions src/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
gittown "github.com/LoyalPotato/sg/src/internal/git-town"
"github.com/LoyalPotato/sg/src/internal/messages"
"github.com/LoyalPotato/sg/src/internal/styles"
"github.com/LoyalPotato/sg/src/internal/utils"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
Expand All @@ -34,7 +35,7 @@ func setupCmd() *cobra.Command {
return cmd
}

func runSetup(cmd *cobra.Command, args []string) {
func runSetup(_ *cobra.Command, _ []string) {
fmt.Printf(messages.Setup_TokenInfo, styles.FaintItalic("https://github.com/settings/tokens"))
prompt := getTokenPrompt()
token, err := prompt.Run()
Expand All @@ -43,8 +44,15 @@ func runSetup(cmd *cobra.Command, args []string) {
os.Exit(1)
}
branchName := getMainBranch()
gittown.AddGithubToken(token, true)
gittown.AddMainBranchConfig(branchName, true)
err = gittown.AddGithubToken(token, true)
if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}

err = gittown.AddMainBranchConfig(branchName, true)
if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}
fmt.Printf("\n"+messages.Setup_Finish, styles.Green("sg start"))
}

Expand Down
35 changes: 25 additions & 10 deletions src/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func startCtor() Start {
}
}

func runStart(cmd *cobra.Command, args []string) {
func runStart(_ *cobra.Command, _ []string) {
start := startCtor()
start.printStory()
start.refactor()
Expand All @@ -93,7 +93,12 @@ func (s *Start) printStory() {
func (s *Start) hack(match string, branchName string) {
fmt.Printf(messages.Start_Hack, styles.Faint(s.mainBranch), styles.Green(match))
cmdMatch(match)
gittown.Hack(branchName)

err := gittown.Hack(branchName)
if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}

fmt.Printf(messages.Start_Hack_Finished, styles.Faint(s.mainBranch))
}

Expand All @@ -108,7 +113,10 @@ func (s *Start) refactor() {
func (s *Start) bugfix() {
fmt.Printf(messages.Start_Bugfix, styles.Green(s.bugAppend))
cmdMatch(s.bugAppend)
gittown.Append(s.bugfixBranch)
err := gittown.Append(s.bugfixBranch)
if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}
fmt.Printf(messages.Start_Bugfix_Tree, s.mainBranch)
fmt.Println(messages.Start_Bugfix_Changes)
confirmChanges()
Expand All @@ -117,14 +125,17 @@ func (s *Start) bugfix() {
}

func (s *Start) feature() {
var err error
fmt.Printf(messages.Start_Feature, styles.Green(s.featureAppend))
cmdMatch(s.featureAppend)
gittown.Append(s.featureBranch)
err = gittown.Append(s.featureBranch)
if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}
fmt.Printf(messages.Start_Feature_Tree, s.mainBranch)
fmt.Printf(messages.Start_Feature_Refactor, styles.Green(s.refactorBranch))

// Making changes to refactor
var err error
err = git.Checkout(s.refactorBranch)
if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
Expand Down Expand Up @@ -172,7 +183,6 @@ func (s *Start) shipRefactor() {
cmdMatch(s.refactorShip)

err := gittown.Ship(s.refactorBranch)

if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}
Expand Down Expand Up @@ -205,7 +215,6 @@ func (s *Start) commitAll() {
Default: messages.Git_Default_Commit_Msg,
AllowEdit: false,
})

if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}
Expand All @@ -226,18 +235,24 @@ func cmdMatch(match string) {
Label: "Command",
MatchVal: utils.RemoveNonPrintables(match),
})

if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}
}

func confirmChanges() {
dialog.Confirm("Finished")
var err error
_, err = dialog.Confirm("Finished")
if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}
changes, _ := git.GetNumOfChanges()
for changes < 1 {
fmt.Println(messages.No_Changes)
dialog.Confirm("Finished")
_, err = dialog.Confirm("Finished")
if err != nil {
utils.Exit(fmt.Sprintf(messages.Generic_Error, err), 1)
}
changes, _ = git.GetNumOfChanges()
}
}
15 changes: 8 additions & 7 deletions src/internal/dialog/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type MatchPromptArgs struct {
MatchVal string
}

// Creates a prompt that is valid when the value is the same as the default
// Creates a prompt that is valid when the value is the same as the default.
func Match(args *MatchPromptArgs) (string, error) {
prompt := promptui.Prompt{
Label: args.Label,
Expand All @@ -39,21 +39,22 @@ func Match(args *MatchPromptArgs) (string, error) {

// Confirm prompt
//
// Returns bool value, if confirmed, and error value
// Returns bool value, if confirmed, and error value.
func Confirm(label string) (bool, error) {
prompt := promptui.Prompt{
Label: label,
IsConfirm: true,
HideEntered: true,
}

_, err := prompt.Run()

switch err {
case promptui.ErrAbort:
if errors.Is(err, promptui.ErrAbort) {
return false, err
case promptui.ErrInterrupt:
os.Exit(utils.EXIT_CODE_INTERRUPT)
} else if errors.Is(err, promptui.ErrInterrupt) {
os.Exit(utils.ExitCodeInterrupt)
}

return true, err
}

Expand All @@ -73,7 +74,7 @@ type TextPromptArgs struct {
Templates *promptui.PromptTemplates
}

// Text prompt
// Text prompt.
func Text(args *TextPromptArgs) (output string, err error) {
prompt := promptui.Prompt{
Label: args.Label,
Expand Down
2 changes: 1 addition & 1 deletion src/internal/git-town/append.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gittown

// Calls append cmd of git-town
// Calls append cmd of git-town.
func Append(branch string) error {
return runGitTown("append", branch)
}
2 changes: 1 addition & 1 deletion src/internal/git-town/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
const MainBranchKey = "git-town.main-branch-name"

func AddMainBranchConfig(brachName string, logRun bool) error {
return git.AddConfig(git.GitConfig{
return git.AddConfig(git.Config{
Key: MainBranchKey,
Value: brachName,
LogRun: logRun,
Expand Down
2 changes: 1 addition & 1 deletion src/internal/git-town/hack.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gittown

// Calls hack cmd of git-town
// Calls hack cmd of git-town.
func Hack(branch string) error {
return runGitTown("hack", branch)
}
2 changes: 1 addition & 1 deletion src/internal/git-town/pull_request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gittown

// Calls hack cmd of git-town
// Calls hack cmd of git-town.
func PullRequest() error {
return runGitTown("new-pull-request")
}
2 changes: 1 addition & 1 deletion src/internal/git-town/sync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gittown

// Calls append cmd of git-town
// Calls append cmd of git-town.
func Sync() error {
return runGitTown("sync")
}
2 changes: 1 addition & 1 deletion src/internal/git-town/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
const TokenKey = "git-town.github-token"

func AddGithubToken(token string, logRun bool) error {
return git.AddConfig(git.GitConfig{
return git.AddConfig(git.Config{
Key: TokenKey,
Value: token,
LogRun: logRun,
Expand Down
1 change: 0 additions & 1 deletion src/internal/git/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func GetNumOfChanges() (int, error) {
}

changes, err := utils.LineCounter(strings.NewReader(string(output)))

if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package git

import "github.com/LoyalPotato/sg/src/internal/cli"

// Perform a git commit
// Perform a git commit.
func Commit(msg string) error {
return cli.RunCmd("git", "commit", "-m", msg)
}
12 changes: 6 additions & 6 deletions src/internal/git/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"github.com/LoyalPotato/sg/src/internal/utils"
)

type GitConfig struct {
type Config struct {
Key, Value string
LogRun bool
}

// Add item to local git config
func AddConfig(gitConfig GitConfig) error {
// Add item to local git config.
func AddConfig(gitConfig Config) error {
args := []string{"config", "--add", gitConfig.Key, gitConfig.Value}
if gitConfig.LogRun {
fmt.Printf("Inserting %s into config\n", styles.Faint(args[2]))
Expand All @@ -23,21 +23,21 @@ func AddConfig(gitConfig GitConfig) error {
return cli.RunCmd("git", args...)
}

// Get local single git config
// Get local single git config.
func GetConfig(key string) (string, error) {
out, err := cli.RunCmdWithOutput("git", "config", "--get", key)

return string(out), err
}

// Check if local git config exists
// Check if local git config exists.
func ConfigExists(key string) bool {
_, err := cli.RunCmdWithOutput("git", "config", "--get", key)

return utils.GetExitCode(err) != 1
}

// Delete local git config
// Delete local git config.
func UnsetConfig(key string) error {
return cli.RunCmd("git", "config", "--unset-all", key)
}
2 changes: 1 addition & 1 deletion src/internal/git/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/LoyalPotato/sg/src/internal/cli"
)

// Checks if current folder is a git repository
// Checks if current folder is a git repository.
func IsGitRepo() error {
return cli.RunCmd("git", "rev-parse", "--git-dir")
}
6 changes: 3 additions & 3 deletions src/internal/messages/en.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package messages

const (
NotGitEnv = "Not running in a git repository.\nUse git init to initialize a repository in the current folder."
NoGitTown = "Git town is not installed.\nPlease follow the instructions on the installation page of git town."
Not_Git_Env = "Not running in a git repository.\nUse git init to initialize a repository in the current folder."
No_Git_Town = "Git town is not installed.\nPlease follow the instructions on the installation page of git town."
Git_Commit = "Insert commit message"
Git_Default_Commit_Msg = "Default commit message"
Git_Add_All = "Staging all changes to current branch"
Expand Down Expand Up @@ -105,7 +105,7 @@ Let's merge it into %s. We can do it with the %s command in git-town:
`
Start_Finished = `And that's it!
Stacking workflow allows you to separate PRs that adhere to the single responsability principle, leading to smaller PRs.
Stacking workflow allows you to separate PRs that adhere to the single responsabillity principle, leading to smaller PRs.
It also allows you to work faster since you can continue chaining features while waiting for review/approval.
This workflow can be achieved with just the commands of git, but git-town is a nice wrapper around git with some extra functionalities that make it easier to follow this workflow, like the %s command.
Expand Down
6 changes: 4 additions & 2 deletions src/internal/utils/errors.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package utils

import (
"errors"
"fmt"
"os"
"os/exec"
)

// Find more about exit codes here: https://tldp.org/LDP/abs/html/exitcodes.html
const EXIT_CODE_INTERRUPT int = 130
const ExitCodeInterrupt int = 130

func GetExitCode(err error) int {
if exitError, ok := err.(*exec.ExitError); ok {
var exitError *exec.ExitError
if errors.As(err, &exitError) {
return exitError.ExitCode()
}

Expand Down

0 comments on commit ef134a3

Please sign in to comment.