Skip to content

Commit

Permalink
Create file if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfwulf committed Jul 2, 2020
1 parent 8cd36c4 commit cf2ac10
Show file tree
Hide file tree
Showing 5 changed files with 36,206 additions and 4 deletions.
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
app := &cli.App{
Name: "brahms",
Usage: "visualize your music",
Version: "v0.1.0",
Version: "v0.1.1",
HideHelpCommand: true,
UsageText: "brahms -i path/to/midi [global options]",
OnUsageError: func(context *cli.Context, err error, isSubcommand bool) error {
Expand Down Expand Up @@ -83,7 +83,7 @@ func main() {
},
Action: func(c *cli.Context) error {

input, err := existingFilepath(c.String("in"))
input, err := existingFilepath(c.String("in"), false)
if err != nil {
return err
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func main() {
return err
}
} else {
out, err := existingFilepath(c.String("out"))
out, err := existingFilepath(c.String("out"), true)
if err != nil {
return err
}
Expand All @@ -142,14 +142,22 @@ func main() {

}

func existingFilepath(p string) (path string, err error) {
func existingFilepath(p string, createIfNotExists bool) (path string, err error) {
path, err = filepath.Abs(p)
if err != nil {
return "", err
}
// File does not exist
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) && createIfNotExists{
_, e := os.Create(path)
if e != nil {
return "", e
}
} else {

return "", fmt.Errorf("it seems like there was an issue with the file %s:\n%s", path, err)
}
}
return path, nil
}
Binary file added midi2csv/brahms_social.afpub
Binary file not shown.
Binary file added midi2csv/brahms_social.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cf2ac10

Please sign in to comment.