Skip to content

Commit e849109

Browse files
committed
feat: initial commit 🚀
0 parents  commit e849109

File tree

10 files changed

+430
-0
lines changed

10 files changed

+430
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Nohns CLI
2+
3+
A CLI with commands for automating simple but repetitive time consuming tasks.
4+
5+
## Compatibility
6+
7+
All commands are to be used on Unix like system such as macOS or Linux.
8+
Some commands are meant to only be run on Linux while some are cross platform. You will be warned when running the command.
9+
10+
## Secrets
11+
12+
A .nohns directory will be created at the users directory which will be used to store secrets

cmd/nohns/main.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/user"
7+
"path/filepath"
8+
9+
"github.com/nohns/nohns-cli/flag"
10+
"github.com/nohns/nohns-cli/subcmd"
11+
"github.com/urfave/cli/v2"
12+
)
13+
14+
const (
15+
confDirName = ".nohns"
16+
)
17+
18+
func main() {
19+
20+
// Depedencies
21+
nvf := subcmd.NewNginxVhostFactory()
22+
23+
app := cli.App{
24+
Name: "nohns",
25+
Usage: "A simple CLI for automating repetivite tasks",
26+
EnableBashCompletion: true,
27+
Before: func(c *cli.Context) error {
28+
// Set home directory on context
29+
usr, err := user.Current()
30+
if err != nil {
31+
return fmt.Errorf("could not get current user: %s", err)
32+
}
33+
if err := c.Set(flag.NameHomeDir, usr.HomeDir); err != nil {
34+
return fmt.Errorf("could not set home directory on CLI context: %s", err)
35+
}
36+
37+
// Make sure config directory exists
38+
if err := ensureConfDirIn(usr.HomeDir); err != nil {
39+
return err
40+
}
41+
42+
return nil
43+
},
44+
Flags: []cli.Flag{
45+
&cli.StringFlag{
46+
Name: flag.NameHomeDir,
47+
Usage: "The home directory of the current user",
48+
},
49+
},
50+
Commands: []*cli.Command{
51+
{
52+
Name: "cp-pubkey",
53+
Aliases: []string{"pk"},
54+
Usage: "Copies public key to clipboard",
55+
Action: subcmd.ActionCopyPubSSHKey,
56+
Flags: []cli.Flag{
57+
&cli.StringFlag{
58+
Name: flag.NameSSHKey,
59+
Usage: "The name of the public key file",
60+
Value: flag.DefaultSSHKey,
61+
},
62+
},
63+
},
64+
{
65+
Name: "nginx",
66+
Aliases: []string{"i"},
67+
Usage: "Run nginx related tasks",
68+
Subcommands: subcmd.NginxSubcommands(nvf),
69+
},
70+
},
71+
}
72+
73+
// Run cli
74+
if err := app.Run(os.Args); err != nil {
75+
fmt.Printf("\nerror: %s\n", err)
76+
os.Exit(1)
77+
}
78+
}
79+
80+
// Ensures that a config directory exists
81+
func ensureConfDirIn(basepath string) error {
82+
p := filepath.Join(basepath, confDirName)
83+
fi, err := os.Stat(p)
84+
if os.IsNotExist(err) {
85+
if err := os.Mkdir(p, 0700); err != nil {
86+
return fmt.Errorf("could not create %s: %s", p, err)
87+
}
88+
89+
return nil
90+
}
91+
if err != nil {
92+
return fmt.Errorf("could not create %s: %s", p, err)
93+
}
94+
if !fi.IsDir() {
95+
return fmt.Errorf("%s is not a directory. please remove it and try running the cli again.", p)
96+
}
97+
98+
return nil
99+
}

flag/flag.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package flag
2+
3+
import (
4+
"github.com/urfave/cli/v2"
5+
)
6+
7+
const (
8+
NameHomeDir = "home_dir"
9+
)
10+
11+
func HomeDir(c *cli.Context) string {
12+
return c.String(NameHomeDir)
13+
}

flag/ssh.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package flag
2+
3+
import "github.com/urfave/cli/v2"
4+
5+
const (
6+
NameSSHKey = "key"
7+
DefaultSSHKey = "id_rsa.pub"
8+
)
9+
10+
func SSHKey(c *cli.Context) string {
11+
return c.String(NameSSHKey)
12+
}

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/nohns/nohns-cli
2+
3+
go 1.20
4+
5+
require github.com/urfave/cli v1.22.14
6+
7+
require (
8+
github.com/atotto/clipboard v0.1.4 // indirect
9+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
10+
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
11+
github.com/manifoldco/promptui v0.9.0 // indirect
12+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
13+
github.com/urfave/cli/v2 v2.25.7 // indirect
14+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
15+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
16+
)

go.sum

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
2+
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
3+
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
4+
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
5+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
6+
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
7+
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
8+
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
9+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
10+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
13+
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
14+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
16+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
17+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
19+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
20+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
21+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
22+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
23+
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
24+
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
25+
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
26+
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
27+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
28+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
29+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
30+
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
31+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
32+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
33+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

nohns

5.19 MB
Binary file not shown.

subcmd/assertion.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package subcmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/urfave/cli/v2"
8+
)
9+
10+
func AssertSudo(f cli.ActionFunc) cli.ActionFunc {
11+
return func(c *cli.Context) error {
12+
if os.Geteuid() != 0 {
13+
return fmt.Errorf("this command requires sudo.")
14+
}
15+
16+
if f != nil {
17+
return f(c)
18+
}
19+
20+
return nil
21+
}
22+
}

0 commit comments

Comments
 (0)