Skip to content

Commit

Permalink
Exit 0 if user or story ID can't be found
Browse files Browse the repository at this point in the history
- Print explanation of error
- Let the user get on with their commit
  • Loading branch information
odlp committed Sep 17, 2016
1 parent 7427803 commit 172f56d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,4 @@ You'll see this commit linked in the activity of your story:
## Todo

- Add interactive git hook setup
- Be graceful and don't prevent commits if an error occurs. E.g.
- The user has no started stories
- The API is down or keys are misconfigured
- Cache the author's Tracker initials to make the hook faster (fewer requests)
4 changes: 2 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/odlp/inflight/runner"
)

const currentVersion = "0.1.0"
const currentVersion = "0.1.1"

func main() {
var displayVersion = flag.Bool("version", false, "view version information")
Expand All @@ -21,7 +21,7 @@ func main() {
}

if outputPath == nil || *outputPath == "" {
fmt.Println("Output location for commit message required.")
fmt.Println("Inflight: Output location for commit message required")
os.Exit(1)
}

Expand Down
17 changes: 9 additions & 8 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,10 @@ func NewRunner(outputPath string) Runner {

func (r Runner) Exec() {
u, err := r.Project.FindUserByEmail(r.Config.GitAuthorEmail)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
gracefulExitIfError(err)

s, err := r.Project.FindCurrentStory(u)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
gracefulExitIfError(err)

outputText := fmt.Sprintf("[#%d]\n", s.ID)
r.Writer.WriteToFile(r.Config.OutputPath, outputText)
Expand All @@ -61,3 +55,10 @@ func configWithOutputPath(outputPath string) Config {
TrackerProjectID: projectID,
}
}

func gracefulExitIfError(err error) {
if err != nil {
fmt.Printf("Inflight: %s\n", err.Error())
os.Exit(0)
}
}
10 changes: 4 additions & 6 deletions runner/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ type Writer struct {

func (w Writer) WriteToFile(filePath string, text string) {
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
gracefulExitIfError(err)

defer f.Close()
if _, err = f.WriteString(text); err != nil {
panic(err)
}

_, err = f.WriteString(text)
gracefulExitIfError(err)
}

0 comments on commit 172f56d

Please sign in to comment.