-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
219 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "") | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.