Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add goto init #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ For a git config, you need to have checked out a git repository containing a fil
- `alias remove <alias>`: removes a local alias.
- `find [<label>]`: displays the url for the specified label, if any. If no label is specified, will display *all* labels in the store.
Combine this with `grep` to find a label based on a regex!
- `init`: explicitly create initial config files for goto. Will do nothing if they already exist.
- `open <label>`: opens the url for the label in a browser. It is possible to supply a path to be used with the url.
- For example: if the entry `jira:https://jira.com` existed, one could execute `goto jira/ops/123` and this would result in the url
`https://jira.com/ops/123` being opened in the browser.
- NOTE: Running `goto <label>` is equivalent to running `goto open <label>`. This does mean that a label cannot have the same name as any
of `goto`'s subcommands.
- NOTE: Running `goto <label>` is equivalent to running `goto open <label>`. This does mean that a label cannot have the same name as any of `goto`'s subcommands.
- `remove <label>`: removes the entry for the specified label.
- NOTE: for a `git_config` remove will always pull the configured repo before running.
- `sync`: syncs with the configured remote. For a `json_config` this is a no-op. For a `git_config`, this pulls from the configured remote.
Expand All @@ -47,6 +47,7 @@ make build

to build a binary to `out/goto`. To install the binary, run:


```bash
sudo make install
```
Expand All @@ -59,15 +60,14 @@ make install DEST=<path>

where `<path>` is a directory in `$PATH` and is writable by you.

Create a config directory for goto:
Create config directory and files for goto:

```bash
mkdir -p ~/.config/goto/
goto init
```

Add your config at `~/.config/goto/config.yaml`
Files will be created in `~/.config/goto/`.

Create an initial empty `{}` map in `~/.config/goto/aliases.json` for storing aliases

## Ideas for future development

Expand All @@ -78,6 +78,6 @@ Create an initial empty `{}` map in `~/.config/goto/aliases.json` for storing al
- [X] Local aliases. Similar to the above, it would allow the user to create aliases for labels that are not pushed to a remote. This would prevent polluting the shared repo with duplicate urls. What is short/convenient for one person may not be for another
(e.g. say there is an existing `github:https://github.com` entry. Someone who uses it a lot may want to have
`gh:https://github.com` for quick access).
- [ ] Initialization helpers (creating `~/.config/goto/` along with necessary files, setting up `links.json` in an empty repo).
- [X] Initialization helpers (creating `~/.config/goto/` along with necessary files, setting up `links.json` in an empty repo).
- [ ] Label/command tab-completion.
- [ ] Server mode. Would expose a locally running API that could be used by a browser directly.
18 changes: 18 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(initCmd)
}

var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize config files",
Long: "Initializes goto config files, if they don't exist",
Run: func(cmd *cobra.Command, args []string) {
// do nothing, since the initialization happens in root.go
},
}
28 changes: 27 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/spartan0x117/goto/pkg/storage"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,11 +34,36 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
} else {
home, err := os.UserHomeDir()
configDir := fmt.Sprintf("%s/.config/goto", home)
cobra.CheckErr(err)

viper.AddConfigPath(fmt.Sprintf("%s/.config/goto", home))
viper.AddConfigPath(configDir)
viper.SetConfigType("yaml")
viper.SetConfigName("config")

err = os.MkdirAll(configDir, os.ModePerm)
// don't fail if directory already exists
if err != nil && !os.IsExist(err) {
panic(fmt.Errorf("could not create %v: %w", configDir, err))
}
configPath := filepath.Join(configDir, "config.yaml")
if _, err := os.Stat(configPath); err != nil {
// default container dir for links.json is configDir
jsonStoragePath := filepath.Join(configDir, "links.json")
viper.SetDefault("type", "json")
viper.SetDefault("json_config", map[string]string{"path": jsonStoragePath})
err = viper.WriteConfigAs(configPath)
if err != nil {
panic(fmt.Errorf("%v: %w", configPath, err))
}
}
aliasesPath := filepath.Join(configDir, "aliases.json")
if _, err := os.Stat(aliasesPath); err != nil {
err := os.WriteFile(aliasesPath, []byte("{}\n"), 0600)
if err != nil {
panic(fmt.Errorf("%v: %w", aliasesPath, err))
}
}
}

if err := viper.ReadInConfig(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func main() {
"add": true,
"alias": true,
"find": true,
"init": true,
"open": true,
"remove": true,
"sync": true,
Expand Down