Skip to content

Commit

Permalink
Add colors!
Browse files Browse the repository at this point in the history
  • Loading branch information
monopole committed Nov 12, 2023
1 parent 62c4bb7 commit 640619f
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 42 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
)

require (
github.com/TwiN/go-color v1.4.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/TwiN/go-color v1.4.1 h1:mqG0P/KBgHKVqmtL5ye7K0/Gr4l6hTksPgTgMk3mUzc=
github.com/TwiN/go-color v1.4.1/go.mod h1:WcPf/jtiW95WBIsEeY1Lc/b8aaWoiqQpu5cf8WFxu+s=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
Expand Down
26 changes: 20 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,26 @@ type Config struct {
ServerOpts map[ServerDomain]ServerOpts `yaml:"serverOpts"`
}

// ServerDomain is the domain of the git server (e.g. github.com).
type ServerDomain string

func (d ServerDomain) WithPort(p int) string {
if p > 0 {
return fmt.Sprintf("%s:%d", d, p)
}
return string(d)
// ServerOpts provides details about using the git server
type ServerOpts struct {
// What port?
Port int
// https or ssh?
Scheme string
// How long to wait for a git operation?
Timeout string
}

// OrgName names the "git" organization.
// If the name has "|" characters, then it's parsed.
type OrgName string

// RepoName is the name of a repository, e.g. kubectl
type RepoName string

func (on OrgName) Parse() (file.Path, OrgName, OrgName) {
n := strings.Split(string(on), "|")
if len(n) > 2 {
Expand All @@ -75,4 +84,9 @@ func (on OrgName) Parse() (file.Path, OrgName, OrgName) {
return file.Path(n[0]), OrgName(n[0]), OrgName(n[0])
}

type RepoName string
func (d ServerDomain) WithPort(p int) string {
if p > 0 {
return fmt.Sprintf("%s:%d", d, p)
}
return string(d)
}
7 changes: 0 additions & 7 deletions internal/config/serveropts.go
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
package pkg

// ServerOpts is a serialized form of ServerSpec.
type ServerOpts struct {
Port int
Scheme string
Timeout string
}
2 changes: 1 addition & 1 deletion internal/tree/reponode.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (n *RepoNode) UrlUpstream() string {

func (n *RepoNode) urlSpec(o string) string {
p := path.Join(o, n.Name) + ".git"
if n.ServerSpec().Scheme() == pkg.SchemeHttps {
if n.ServerSpec().Scheme() == SchemeHttps {
// https://github.com/monopole/myrepos.git
return "https://" + n.parent.parent.Domain().WithPort(n.ServerSpec().Port()) + "/" + p
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/scheme.go → internal/tree/scheme.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package tree

type Scheme int

Expand Down
14 changes: 7 additions & 7 deletions internal/tree/serverspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type ServerSpec struct {
port int
scheme pkg.Scheme
scheme Scheme
timeout time.Duration
}

Expand All @@ -21,7 +21,7 @@ func (s *ServerSpec) Timeout() time.Duration {
return s.timeout
}

func (s *ServerSpec) Scheme() pkg.Scheme {
func (s *ServerSpec) Scheme() Scheme {
return s.scheme
}

Expand All @@ -33,7 +33,7 @@ func (s *ServerSpec) Port() int {
func MakeServerSpec() *ServerSpec {
return &ServerSpec{
port: 0,
scheme: pkg.SchemeSsh,
scheme: SchemeSsh,
timeout: 4 * time.Minute,
}
}
Expand All @@ -49,10 +49,10 @@ func FromServerOpts(s *pkg.ServerOpts) (result *ServerSpec, err error) {
}
if s.Scheme != "" {
switch s.Scheme {
case pkg.SchemeHttps.String():
result.scheme = pkg.SchemeHttps
case pkg.SchemeSsh.String():
result.scheme = pkg.SchemeSsh
case SchemeHttps.String():
result.scheme = SchemeHttps
case SchemeSsh.String():
result.scheme = SchemeSsh
default:
return nil, fmt.Errorf("unknown scheme %q in serverSpec opts", s.Scheme)
}
Expand Down
37 changes: 20 additions & 17 deletions internal/visitor/cloner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@ package visitor

import (
"fmt"
"github.com/TwiN/go-color"
"github.com/monopole/myrepos/internal/runner"
"github.com/monopole/myrepos/internal/tree"
)

type Cloner struct {
Err error
FatalErr error
// Output of most recent git command.
gr *runner.Runner
lastErr error
fatalErr error
gr *runner.Runner
}

func (v *Cloner) fatal(e error) {
v.FatalErr, v.Err = e, e
v.fatalErr, v.lastErr = e, e
}

func (v *Cloner) Err() error {
return v.lastErr
}

func (v *Cloner) VisitRootNode(n *tree.RootNode) {
if v.FatalErr != nil {
if v.fatalErr != nil {
return
}
exists, isDir := n.AbsPath().Exists()
if !exists {
// Make it for them instead of complain?
// Make it for instead of complain?
// Could trigger a bunch of work if it's just a typo.
v.fatal(fmt.Errorf("if you want the root dir %q, make it first", n.AbsPath()))
return
}
Expand All @@ -32,35 +37,35 @@ func (v *Cloner) VisitRootNode(n *tree.RootNode) {
return
}
indent(0)
fmt.Println(n.AbsPath())
fmt.Println(color.InBlackOverWhite(n.AbsPath()))
}

func (v *Cloner) VisitServerNode(n *tree.ServerNode) {
if v.FatalErr != nil {
if v.fatalErr != nil {
return
}
if exists, isDir := n.AbsPath().Exists(); exists && !isDir {
v.fatal(fmt.Errorf("%q exists but isn't a directory", n.AbsPath()))
return
}
indent(1)
fmt.Println(n.Domain())
fmt.Println(color.YellowBackground, n.Domain(), color.Reset)
}

func (v *Cloner) VisitOrgNode(n *tree.OrgNode) {
if v.FatalErr != nil {
if v.fatalErr != nil {
return
}
if exists, isDir := n.AbsPath().Exists(); exists && !isDir {
v.fatal(fmt.Errorf("%q exists but isn't a directory", n.AbsPath()))
return
}
indent(2)
fmt.Printf("%s|%s|%s\n", n.NameDir(), n.NameOrigin(), n.NameUpstream())
fmt.Println(color.CyanBackground, n.NameDir(), color.Reset)
}

func (v *Cloner) VisitRepoNode(n *tree.RepoNode) {
if v.FatalErr != nil {
if v.fatalErr != nil {
return
}
exists, isDir := n.AbsPath().Exists()
Expand Down Expand Up @@ -88,16 +93,14 @@ func (v *Cloner) VisitRepoNode(n *tree.RepoNode) {
return
}
v.reportStatus(n, outcome, status)

fmt.Println(n.Name)
}

func (v *Cloner) reportErr(n *tree.RepoNode, err error) {
v.Err = err
v.lastErr = err
v.reportStatus(n, Oops, err.Error())
}

func (v *Cloner) reportStatus(n *tree.RepoNode, outcome Outcome, status string) {
indent(3)
fmt.Printf("%20s %20s %s\n", n.Name, outcome, status)
fmt.Printf("%30s%30s %s\n", n.Name, outcome, status)
}
2 changes: 1 addition & 1 deletion internal/visitor/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (v *Cloner) LastLog() (string, error) {
}
if err := v.gr.Run(
cmdLog,
`--pretty=format:"%<(20)%ad%>(24)%an : %s"`,
`--pretty=format:"%<(26)%ad%>(30)%an : %s"`,
`--date=human`,
`--abbrev=8`,
`--max-count=1`,
Expand Down
9 changes: 8 additions & 1 deletion internal/visitor/outcome.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package visitor

import "github.com/TwiN/go-color"

type Outcome int

const (
Expand All @@ -10,5 +12,10 @@ const (
)

func (o Outcome) String() string {
return []string{"error", "rebased to", "cloned to latest at", "no change since"}[o]
return []string{
color.Red + "error" + color.Reset,
color.Blue + "rebased to" + color.Reset,
color.Green + "cloned to latest at" + color.Reset,
color.Green + "no change since" + color.Reset,
}[o]
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newCommand() *cobra.Command {
return err
}
t.Accept(&v)
return v.Err
return v.Err()
},
SilenceUsage: true,
}
Expand Down

0 comments on commit 640619f

Please sign in to comment.