Skip to content

Commit 84cf853

Browse files
committedMar 25, 2022
✨ command print-banner
1 parent 8c629c9 commit 84cf853

File tree

10 files changed

+214
-25
lines changed

10 files changed

+214
-25
lines changed
 

‎README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
My terminal assistant
44

5-
- [ ] 打印自定义 banner
5+
## :fried_egg: 食用指南
6+
7+
```sh
8+
go install github.com/naiba/nb@latest
9+
nb -h
10+
```
11+
12+
## To do
13+
14+
- [x] 打印自定义 banner
615
- [ ] 不同仓库下的 git 账户
716
- [ ] SSH 主机管理
817
- [ ] git/ssh/scp/rsync 代理配置

‎assets/assets.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package assets
2+
3+
import _ "embed"
4+
5+
//go:embed nyancat.txt
6+
var Nyancat string

‎cmd/print_banner.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"os"
6+
7+
"github.com/dimiro1/banner"
8+
"github.com/naiba/nb/assets"
9+
"github.com/naiba/nb/singleton"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func init() {
14+
rootCmd.AddCommand(printBannerCmd)
15+
}
16+
17+
var printBannerCmd = &cobra.Command{
18+
Use: "print-banner",
19+
Short: "可用在终端启动时打印 banner",
20+
Run: func(cmd *cobra.Command, args []string) {
21+
if singleton.Config.Banner != "" {
22+
banner.Init(os.Stdout, true, true, bytes.NewBufferString(singleton.Config.Banner))
23+
} else {
24+
banner.Init(os.Stdout, true, true, bytes.NewBufferString(assets.Nyancat))
25+
}
26+
},
27+
}

‎cmd/root.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var rootCmd = &cobra.Command{
11+
Use: "nb",
12+
Short: "Nb is a very nb",
13+
Run: func(cmd *cobra.Command, args []string) {
14+
// Do Stuff Here
15+
},
16+
}
17+
18+
func Execute() {
19+
if err := rootCmd.Execute(); err != nil {
20+
fmt.Fprintln(os.Stderr, err)
21+
os.Exit(1)
22+
}
23+
}

‎config.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
banner: |
2+
{{ .AnsiColor.Green }}
3+
============ naiba's workflow ============
4+
{{ .AnsiColor.Red }}
5+
███▄ █ ▄▄▄ ██▓ ▄▄▄▄ ▄▄▄
6+
██ ▀█ █ ▒████▄ ▓██▒▓█████▄ ▒████▄
7+
▓██ ▀█ ██▒▒██ ▀█▄ ▒██▒▒██▒ ▄██▒██ ▀█▄
8+
▓██▒ ▐▌██▒░██▄▄▄▄██ ░██░▒██░█▀ ░██▄▄▄▄██
9+
▒██░ ▓██░ ▓█ ▓██▒░██░░▓█ ▀█▓ ▓█ ▓██▒
10+
░ ▒░ ▒ ▒ ▒▒ ▓▒█░░▓ ░▒▓███▀▒ ▒▒ ▓▒█░
11+
░ ░░ ░ ▒░ ▒ ▒▒ ░ ▒ ░▒░▒ ░ ▒ ▒▒ ░
12+
░ ░ ░ ░ ▒ ▒ ░ ░ ░ ░ ▒
13+
░ ░ ░ ░ ░ ░ ░
14+
15+
{{ .AnsiColor.Green }}============ Now: {{ .Now "Monday, 2 Jan 2006" }} ============
16+
{{ .AnsiColor.Green }}

‎go.mod

+21-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@ module github.com/naiba/nb
22

33
go 1.18
44

5-
require github.com/dimiro1/banner v1.1.0
5+
require (
6+
github.com/dimiro1/banner v1.1.0
7+
github.com/spf13/cobra v1.4.0
8+
github.com/spf13/viper v1.10.1
9+
)
610

711
require (
812
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2 // indirect
9-
github.com/mattn/go-isatty v0.0.10 // indirect
10-
golang.org/x/sys v0.0.0-20191008105621-543471e840be // indirect
13+
github.com/fsnotify/fsnotify v1.5.1 // indirect
14+
github.com/hashicorp/hcl v1.0.0 // indirect
15+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
16+
github.com/magiconair/properties v1.8.5 // indirect
17+
github.com/mattn/go-isatty v0.0.14 // indirect
18+
github.com/mitchellh/mapstructure v1.4.3 // indirect
19+
github.com/pelletier/go-toml v1.9.4 // indirect
20+
github.com/spf13/afero v1.6.0 // indirect
21+
github.com/spf13/cast v1.4.1 // indirect
22+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
23+
github.com/spf13/pflag v1.0.5 // indirect
24+
github.com/subosito/gotenv v1.2.0 // indirect
25+
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
26+
golang.org/x/text v0.3.7 // indirect
27+
gopkg.in/ini.v1 v1.66.2 // indirect
28+
gopkg.in/yaml.v2 v2.4.0 // indirect
1129
)

‎go.sum

+63-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,72 @@
11
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2 h1:tjT4Jp4gxECvsJcYpAMtW2I3YqzBTPuB67OejxXs86s=
22
github.com/common-nighthawk/go-figure v0.0.0-20200609044655-c4b36f998cf2/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
37
github.com/dimiro1/banner v1.1.0 h1:TSfy+FsPIIGLzaMPOt52KrEed/omwFO1P15VA8PMUh0=
48
github.com/dimiro1/banner v1.1.0/go.mod h1:tbL318TJiUaHxOUNN+jnlvFSgsh/RX7iJaQrGgOiTco=
9+
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
10+
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
11+
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
12+
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
13+
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
14+
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
15+
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
16+
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
17+
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
518
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
619
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
7-
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
820
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
21+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
22+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
23+
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
24+
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
25+
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
26+
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
27+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
28+
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
29+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
30+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
31+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
32+
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
33+
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
34+
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
35+
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
36+
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
37+
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
38+
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
39+
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
40+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
41+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
42+
github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk=
43+
github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU=
44+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
45+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
46+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
47+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
48+
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
49+
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
50+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
51+
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
52+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
53+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
954
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
10-
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
55+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1156
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
57+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
58+
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 h1:5hpz5aRr+W1erYCL5JRhSUBJRph7l9XkNveoExlrKYk=
59+
golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
60+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
61+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
62+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
63+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
64+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
65+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
66+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
67+
gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
68+
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
69+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
70+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
71+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
72+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=

‎main.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
11
package main
22

33
import (
4-
"bytes"
5-
"flag"
6-
"os"
7-
8-
_ "embed"
9-
10-
"github.com/dimiro1/banner"
4+
"github.com/naiba/nb/cmd"
115
)
126

13-
//go:embed assets/nyancat.txt
14-
var nyancat string
15-
16-
var printBanner bool
17-
187
func main() {
19-
flag.BoolVar(&printBanner, "banner", false, "可用在终端启动时打印 banner")
20-
flag.Parse()
21-
22-
if printBanner {
23-
banner.Init(os.Stdout, true, true, bytes.NewBufferString(nyancat))
24-
return
25-
}
8+
cmd.Execute()
269
}

‎model/config.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package model
2+
3+
import (
4+
"github.com/spf13/viper"
5+
)
6+
7+
type Config struct {
8+
Banner string
9+
}
10+
11+
func ReadInConfig(path string) (*Config, error) {
12+
viper.SetConfigName("config")
13+
viper.SetConfigType("yaml")
14+
if path != "" {
15+
viper.AddConfigPath(path)
16+
}
17+
viper.AddConfigPath("$HOME/.appname")
18+
viper.AddConfigPath(".")
19+
err := viper.ReadInConfig()
20+
if err != nil {
21+
return nil, err
22+
}
23+
var config Config
24+
err = viper.Unmarshal(&config)
25+
if err != nil {
26+
return nil, err
27+
}
28+
return &config, nil
29+
}

‎singleton/singleton.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package singleton
2+
3+
import (
4+
"os"
5+
6+
"github.com/naiba/nb/model"
7+
)
8+
9+
var Config *model.Config
10+
11+
func init() {
12+
var err error
13+
Config, err = model.ReadInConfig(os.Getenv("CONFIG_PATH"))
14+
if err != nil {
15+
panic(err)
16+
}
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.