Skip to content

Commit fb8ca92

Browse files
committed
Add create shelf command
1 parent 419adec commit fb8ca92

File tree

7 files changed

+140
-0
lines changed

7 files changed

+140
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
# Dependency directories (remove the comment below to include it)
1616
# vendor/
1717

18+
shelf.bin

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build:
2+
go build -o shelf.bin ./cmd/shelf

cmd/shelf/actions.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"os/exec"
8+
"path"
9+
10+
"github.com/urfave/cli/v2"
11+
)
12+
13+
// CreateShelf creates a new shelf
14+
func CreateShelf(cliCtx *cli.Context) error {
15+
// Get Home Directory path
16+
home, err := GetHomeDirectory()
17+
if err != nil {
18+
return err
19+
}
20+
21+
shelfName := cliCtx.Args().First()
22+
23+
if shelfName == "" {
24+
return errors.New("Shelf name has to be given")
25+
}
26+
27+
shelfPath := path.Join(home, shelfName)
28+
29+
err = os.Mkdir(shelfPath, 0755)
30+
if err != nil {
31+
if os.IsExist(err) {
32+
return fmt.Errorf("Shelf named: \"%s\" already exist", shelfName)
33+
}
34+
return err
35+
}
36+
37+
err = os.Chdir(shelfPath)
38+
if err != nil {
39+
return err
40+
}
41+
42+
// Initialize git in the shelf
43+
cmd := exec.Command("git", "init")
44+
err = cmd.Run()
45+
if err != nil {
46+
return err
47+
}
48+
49+
fmt.Printf("[*] Created a shelf named: %s", shelfName)
50+
51+
return nil
52+
}

cmd/shelf/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
cli "github.com/urfave/cli/v2" // imports as package "cli"
5+
"log"
6+
"os"
7+
)
8+
9+
func main() {
10+
app := &cli.App{
11+
Commands: []*cli.Command{
12+
{
13+
Name: "create",
14+
Aliases: []string{"c"},
15+
Usage: "complete a task on the list",
16+
Action: CreateShelf,
17+
},
18+
},
19+
Name: "shelf",
20+
Description: "A Good Symlinks Manager",
21+
}
22+
23+
err := app.Run(os.Args)
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
}

cmd/shelf/utils.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path"
6+
"runtime"
7+
)
8+
9+
// GetHomeDirectory gets the directory in user's home folder
10+
// where all shelves are stored. If a directory doesn't exist it creates one.
11+
func GetHomeDirectory() (string, error) {
12+
// Get Home Directory path
13+
home := getHomeDirectoryPath()
14+
_, err := os.Stat(home)
15+
if err != nil {
16+
if os.IsNotExist(err) {
17+
err = os.Mkdir(home, 0755)
18+
if err != nil {
19+
return "", err
20+
}
21+
return home, nil
22+
}
23+
return "", err
24+
}
25+
26+
return home, nil
27+
}
28+
29+
func getHomeDirectoryPath() string {
30+
var (
31+
home string
32+
)
33+
if runtime.GOOS == "linux" {
34+
home = os.Getenv("XDG_CONFIG_HOME")
35+
if home == "" {
36+
home = os.Getenv("HOME")
37+
}
38+
}
39+
return path.Join(home, ".shelves")
40+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/iamd3vil/shelf
2+
3+
go 1.14
4+
5+
require github.com/urfave/cli/v2 v2.2.0

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
7+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8+
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
9+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
10+
github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
11+
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
12+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
13+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)