Skip to content

Commit d583e2f

Browse files
Merge pull request #7 from RohitRavindra-dev/feat/v1/status
feat: status command shipped
2 parents d65b1f3 + 1257b0b commit d583e2f

4 files changed

Lines changed: 125 additions & 7 deletions

File tree

cmd/status.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
import (
4+
"github.com/RohitRavindra-dev/devlocal/internal/filesystem"
5+
"github.com/RohitRavindra-dev/devlocal/internal/service/status"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var statusCmd = &cobra.Command{
10+
Use: "status",
11+
Short: "View the current status of devlocal setup",
12+
PreRunE: func(cmd *cobra.Command, args []string) error {
13+
return filesystem.ValidateDevLocalFilesystem()
14+
},
15+
RunE: func(cmd *cobra.Command, args []string) error {
16+
return status.Run()
17+
},
18+
}
19+
20+
func init() {
21+
rootCmd.AddCommand(statusCmd)
22+
}

internal/filesystem/utils.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
package filesystem
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67

78
"github.com/RohitRavindra-dev/devlocal/internal/config"
89
"gopkg.in/yaml.v3"
910
)
1011

11-
func exists(path string) (bool, error) {
12-
_, err := os.Stat(path)
12+
func FileExists(path string) (bool, error) {
13+
info, err := os.Stat(path)
1314

14-
if err == nil {
15-
return true, nil
15+
if os.IsNotExist(err) {
16+
return false, fmt.Errorf("%s does not exist", path)
1617
}
1718

18-
if os.IsNotExist(err) {
19-
return false, nil
19+
if err != nil {
20+
return false, err
21+
}
22+
23+
if info.IsDir() {
24+
return false, fmt.Errorf("%s is a directory, expected a file", path)
2025
}
2126

22-
return false, err
27+
return true, nil
2328
}
2429

2530
func LoadDevlocalConfig() (*config.DevlocalConfigYaml, error) {

internal/git/worktrees.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,18 @@ func sanitizeToTrackedFiles(files []string) []string {
7474
return filesTracked
7575

7676
}
77+
78+
func IsSkipWorktree(file string) (bool, error) {
79+
out, err := exec.Command(
80+
"git",
81+
"ls-files",
82+
"-v",
83+
file,
84+
).CombinedOutput()
85+
86+
if err != nil {
87+
return false, err
88+
}
89+
90+
return len(out) > 0 && out[0] == 'S', nil
91+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package status
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/RohitRavindra-dev/devlocal/internal/config"
7+
"github.com/RohitRavindra-dev/devlocal/internal/filesystem"
8+
"github.com/RohitRavindra-dev/devlocal/internal/git"
9+
)
10+
11+
func displayConfigVersion(version int) {
12+
fmt.Printf("[Version] %d \n", version)
13+
}
14+
15+
func displayOverlookStatus(fileNames []string) {
16+
fmt.Println("[Overlooked files]")
17+
if len(fileNames) == 0 {
18+
fmt.Println("\t- No files registed in config for overlooking!")
19+
return
20+
}
21+
for _, filename := range fileNames {
22+
exists, err := filesystem.FileExists(filename)
23+
overlookStatus := "✗"
24+
overlookComment := "Being tracked"
25+
if exists {
26+
27+
if isOverlooked, _ := git.IsSkipWorktree(filename); isOverlooked {
28+
overlookStatus = "✓"
29+
overlookComment = "Overlooked"
30+
}
31+
32+
} else {
33+
overlookComment = err.Error()
34+
}
35+
36+
fmt.Printf("\t%s %s \n\t\t- %s\n", overlookStatus, filename, overlookComment)
37+
38+
}
39+
}
40+
41+
func displayPatchesStatus(patches []string) {
42+
fmt.Println("[Patches]")
43+
if len(patches) == 0 {
44+
fmt.Println("\t- No patches registed in config for patching!")
45+
return
46+
}
47+
}
48+
49+
func displayDevlocalConfig(config *config.DevlocalConfigYaml) error {
50+
fmt.Println("\n===============================")
51+
fmt.Println("| devlocal [Status] |")
52+
fmt.Printf("===============================\n\n")
53+
displayConfigVersion(config.Version)
54+
fmt.Println()
55+
displayOverlookStatus(config.Overlook)
56+
fmt.Println()
57+
displayPatchesStatus(config.Patches)
58+
fmt.Printf("===============================\n\n")
59+
60+
return nil
61+
}
62+
63+
func Run() error {
64+
65+
//load config
66+
config, err := filesystem.LoadDevlocalConfig()
67+
if err != nil {
68+
return err
69+
}
70+
71+
if err := displayDevlocalConfig(config); err != nil {
72+
return err
73+
}
74+
75+
return nil
76+
}

0 commit comments

Comments
 (0)