Skip to content

Commit

Permalink
Switching to cobra
Browse files Browse the repository at this point in the history
  • Loading branch information
nyarly committed Sep 18, 2018
1 parent 2a59630 commit 2e55a57
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 166 deletions.
19 changes: 17 additions & 2 deletions cmdadd.go → cmd/add.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package main
package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

func doAdd() error {
func init() {
rootCmd.AddCommand(addCmd)
}

var addCmd = &cobra.Command{
Use: "add",
Short: "add the current directory to the commute list",
Long: longUsage(`Records the origin or upstream remote of the current git workspace
in the commute list (if not already present) and marks the current directory
as the canonical location of that repo locally.`),
RunE: addFn,
}

func addFn(cmd *cobra.Command, args []string) error {
root, err := getRepoRoot()
if err != nil {
return err
Expand Down
36 changes: 36 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

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

var listCmd = &cobra.Command{
Use: "list",
Short: "list known repos and their status",
Long: longUsage(`For every known repo, either prints the local workspace path,
or an "missing" error to stderr.`),
RunE: listFn,
}

func listFn(cmd *cobra.Command, args []string) error {
for _, remote := range cfg.Remotes {
_, err := os.Stat(remote.linkPath())
if err != nil {
fmt.Fprintf(os.Stderr, "%s -> MISSING\n", remote)
continue
}
p, err := remote.localPath()
if err != nil {
fmt.Fprintf(os.Stderr, "%s : %s\n", remote, err)
}
fmt.Printf("%s\n", p)
}
return nil
}
2 changes: 1 addition & 1 deletion remotes.go → cmd/remotes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"os"
Expand Down
16 changes: 14 additions & 2 deletions cmdrm.go → cmd/rm.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package main
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

func doRemove(args []string) error {
func init() {
rootCmd.AddCommand(rmCmd)
}

var rmCmd = &cobra.Command{
Use: "rm",
Short: "remote repos from tracking",
RunE: rmFn,
}

func rmFn(cmd *cobra.Command, args []string) error {
var remotes []gitRemote
var found bool
if len(args) == 0 {
Expand Down
33 changes: 33 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

// Execute runs the command
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

var rootCmd = &cobra.Command{
Use: "commute",
Short: "commute maintains the mapping of remote repositories to local workspaces",
Long: longUsage(`A utility for keeping track of where repos are on you work machine,
suitable for use by humans and scripts.`),
PersistentPreRunE: setupStuff,
}

func setupStuff(cmd *cobra.Command, args []string) error {
err := setupPaths()
if err != nil {
return err
}

return loadConfig()
}
116 changes: 116 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"regexp"

"github.com/samsalisbury/yaml"
)

const (
relConfigDir = ".config/commute"
relConfigFile = "config.yaml"
docs = `commute: transit git projects back and forth
Usage:
commute list Ensure that the config maps to projects
commute add Add the current git project
commute rm [remote] Remove the current project (or named remote)
from the commute config.
`
)

type (
config struct {
Remotes []remote
}

remote string
)

var (
configDir string
configFile string
cfg config
remoteNameRE = regexp.MustCompile(`([^/:]+/[^/.]+)(?:\.git)?$`)
fieldsRE = regexp.MustCompile(`\s+`)
)

func (r *remote) name() string {
m := remoteNameRE.FindStringSubmatch(string(*r))
if m == nil || len(m) < 2 {
panic("Badly formatted git remote: " + string(*r))
}
return m[1]
}

func (r *remote) linkPath() string {
return filepath.Join(configDir, r.name())
}

func (r *remote) localPath() (string, error) {
return os.Readlink(r.linkPath())
}

func setupPaths() error {
u, err := user.Current()
if err != nil {
return err
}

configDir = filepath.Join(u.HomeDir, relConfigDir)
configFile = filepath.Join(configDir, relConfigFile)
return nil
}

func loadConfig() error {
f, err := os.Open(configFile)
if err != nil {
return err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}

err = yaml.Unmarshal(b, &cfg)
if err != nil {
return err
}

return nil
}

func lookup(start, tgt string) (string, error) {
for from, _ := filepath.Abs(start); !(from == "" || from == "/"); from = filepath.Dir(from) {
cb := filepath.Join(from, tgt)
_, err := os.Lstat(cb)
if err == nil {
return from, nil
}
}
return "", fmt.Errorf("No %s found above %s", tgt, start)
}

func (c *config) save() error {
b, err := yaml.Marshal(c)
if err != nil {
return err
}
f, err := os.Create(configFile)
if err != nil {
return err
}
defer f.Close()
f.Write(b)
return nil
}

var longFix = regexp.MustCompile(`(?m)^[ \t]*`)

func longUsage(s string) string {
return longFix.ReplaceAllString(s, "")
}
22 changes: 0 additions & 22 deletions cmdlist.go

This file was deleted.

Loading

0 comments on commit 2e55a57

Please sign in to comment.