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

pull/73/head
Mickaël Menu 3 years ago committed by GitHub
parent 977625bb3d
commit 8c7ec93ca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### 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)).
* 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

@ -59,12 +59,14 @@ func (cmd *ShowHelp) Run(container *cli.Container) error {
}
func main() {
args := os.Args[1:]
// Create the dependency graph.
container, err := cli.NewContainer(Version)
fatalIfError(err)
// Open the notebook if there's any.
dirs, err := parseDirs()
dirs, args, err := parseDirs(args)
fatalIfError(err)
searchDirs, err := notebookSearchDirs(dirs)
fatalIfError(err)
@ -72,10 +74,13 @@ func main() {
fatalIfError(err)
// Run the alias or command.
if isAlias, err := runAlias(container, os.Args[1:]); isAlias {
if isAlias, err := runAlias(container, args); isAlias {
fatalIfError(err)
} 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`
// command, otherwise it would hide the stats.
@ -86,7 +91,7 @@ func main() {
}
}
err := ctx.Run(container)
err = ctx.Run(container)
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
// 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 err error
findFlag := func(long string, short string) (string, error) {
findFlag := func(long string, short string, args []string) (string, []string, error) {
newArgs := []string{}
foundFlag := ""
for _, arg := range os.Args {
for i, arg := range args {
if arg == long || (short != "" && arg == short) {
foundFlag = arg
} 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 != "" {
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 {
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 {
return d, err
return d, args, err
}
return d, nil
return d, args, nil
}

Loading…
Cancel
Save