Allow setting the --working-dir and --notebook-dir flags before the subcommand when using aliases (#67)

This commit is contained in:
Mickaël Menu 2021-08-07 18:56:26 +02:00 committed by GitHub
parent 977625bb3d
commit 8c7ec93ca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 15 deletions

View File

@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Added ### Added
* Use the `{{abs-path}}` template variable when [formatting notes](docs/template-format.md) to print the absolute path to the note (contributed by [@pstuifzand](https://github.com/mickael-menu/zk/pull/60)). * Use the `{{abs-path}}` template variable when [formatting notes](docs/template-format.md) to print the absolute path to the note (contributed by [@pstuifzand](https://github.com/mickael-menu/zk/pull/60)).
* Allow setting the `--working-dir` and `--notebook-dir` flags before the `zk` subcommand when using aliases, e.g. `zk -W ~/notes my-alias`.
## 0.6.0 ## 0.6.0

41
main.go
View File

@ -59,12 +59,14 @@ func (cmd *ShowHelp) Run(container *cli.Container) error {
} }
func main() { func main() {
args := os.Args[1:]
// Create the dependency graph. // Create the dependency graph.
container, err := cli.NewContainer(Version) container, err := cli.NewContainer(Version)
fatalIfError(err) fatalIfError(err)
// Open the notebook if there's any. // Open the notebook if there's any.
dirs, err := parseDirs() dirs, args, err := parseDirs(args)
fatalIfError(err) fatalIfError(err)
searchDirs, err := notebookSearchDirs(dirs) searchDirs, err := notebookSearchDirs(dirs)
fatalIfError(err) fatalIfError(err)
@ -72,10 +74,13 @@ func main() {
fatalIfError(err) fatalIfError(err)
// Run the alias or command. // Run the alias or command.
if isAlias, err := runAlias(container, os.Args[1:]); isAlias { if isAlias, err := runAlias(container, args); isAlias {
fatalIfError(err) fatalIfError(err)
} else { } else {
ctx := kong.Parse(&root, options(container)...) parser, err := kong.New(&root, options(container)...)
fatalIfError(err)
ctx, err := parser.Parse(args)
fatalIfError(err)
// Index the current notebook except if the user is running the `index` // Index the current notebook except if the user is running the `index`
// command, otherwise it would hide the stats. // command, otherwise it would hide the stats.
@ -86,7 +91,7 @@ func main() {
} }
} }
err := ctx.Run(container) err = ctx.Run(container)
ctx.FatalIfErrorf(err) ctx.FatalIfErrorf(err)
} }
} }
@ -213,33 +218,39 @@ func notebookSearchDirs(dirs cli.Dirs) ([]cli.Dirs, error) {
// //
// We need to parse these flags before Kong, because we might need it to // We need to parse these flags before Kong, because we might need it to
// resolve zk command aliases before parsing the CLI. // resolve zk command aliases before parsing the CLI.
func parseDirs() (cli.Dirs, error) { func parseDirs(args []string) (cli.Dirs, []string, error) {
var d cli.Dirs var d cli.Dirs
var err error var err error
findFlag := func(long string, short string) (string, error) { findFlag := func(long string, short string, args []string) (string, []string, error) {
newArgs := []string{}
foundFlag := "" foundFlag := ""
for _, arg := range os.Args { for i, arg := range args {
if arg == long || (short != "" && arg == short) { if arg == long || (short != "" && arg == short) {
foundFlag = arg foundFlag = arg
} else if foundFlag != "" { } else if foundFlag != "" {
return filepath.Abs(arg) newArgs = append(newArgs, args[i+1:]...)
path, err := filepath.Abs(arg)
return path, newArgs, err
} else {
newArgs = append(newArgs, arg)
} }
} }
if foundFlag != "" { if foundFlag != "" {
return "", errors.New(foundFlag + " requires a path argument") return "", newArgs, errors.New(foundFlag + " requires a path argument")
} }
return "", nil return "", newArgs, nil
} }
d.NotebookDir, err = findFlag("--notebook-dir", "") d.NotebookDir, args, err = findFlag("--notebook-dir", "", args)
if err != nil { if err != nil {
return d, err return d, args, err
} }
d.WorkingDir, err = findFlag("--working-dir", "-W") d.WorkingDir, args, err = findFlag("--working-dir", "-W", args)
if err != nil { if err != nil {
return d, err return d, args, err
} }
return d, nil return d, args, nil
} }