Skip to content

Commit a50f3de

Browse files
Merge pull request #3 from RohitRavindra-dev/feature/v1/devlocal-apply
Feature/v1/devlocal apply
2 parents 31c7306 + 4a7bcb3 commit a50f3de

9 files changed

Lines changed: 200 additions & 1 deletion

File tree

cmd/apply.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/RohitRavindra-dev/devlocal/internal/filesystem"
7+
"github.com/spf13/cobra"
8+
9+
"github.com/RohitRavindra-dev/devlocal/internal/orchestration/apply"
10+
)
11+
12+
var applyCmd = &cobra.Command{
13+
Use: "apply",
14+
Short: "Apply .devlocal changes that makes the project ready for local developement",
15+
PreRunE: func(cmd *cobra.Command, args []string) error {
16+
fmt.Println("Attempting to apply devlocal changes")
17+
// check for setup
18+
return filesystem.ValidateDevLocalFilesystem()
19+
},
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
return apply.Run()
22+
},
23+
}
24+
25+
func init() {
26+
rootCmd.AddCommand(applyCmd)
27+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ go 1.26.4
44

55
require github.com/spf13/cobra v1.10.2 // direct
66

7+
require gopkg.in/yaml.v3 v3.0.1
8+
79
require (
810
github.com/inconshreveable/mousetrap v1.1.0 // indirect
911
github.com/spf13/pflag v1.0.9 // indirect
10-
gopkg.in/yaml.v3 v3.0.1 // indirect
1112
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiT
77
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
88
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
99
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1011
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1112
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1213
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/config/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package config
22

3+
// APP constants
34
const (
45
APP_NAME = "devlocal"
56
PROJECT_ROOT = ".devlocal"

internal/filesystem/initialization.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,34 @@ func InitilizeDevLocalFilesystem() error {
7070
}
7171
return nil
7272
}
73+
74+
func ValidateDevLocalFilesystem() error {
75+
info, err := os.Stat(config.PROJECT_ROOT)
76+
77+
if os.IsNotExist(err) {
78+
return fmt.Errorf("%s does not exist, please run `devlocal init`", config.PROJECT_ROOT)
79+
}
80+
81+
if err != nil {
82+
return err
83+
}
84+
85+
if !info.IsDir() {
86+
return fmt.Errorf("%s is not a directory, run `devlocal cleanup` and re-reun `devlocal install`", config.PROJECT_ROOT)
87+
}
88+
89+
for _, file := range []string{config.CONFIG_FILE_NAME} {
90+
path := filepath.Join(config.PROJECT_ROOT, file)
91+
92+
info, err := os.Stat(path)
93+
if err != nil {
94+
return fmt.Errorf("missing required file: %s, someone done messed up! My advice, setup again", file)
95+
}
96+
97+
if info.IsDir() {
98+
return fmt.Errorf("%s should be a file, someone done messed up! My advice, setup again", file)
99+
}
100+
}
101+
102+
return nil
103+
}

internal/filesystem/utils.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package filesystem
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/RohitRavindra-dev/devlocal/internal/config"
8+
"gopkg.in/yaml.v3"
9+
)
10+
11+
func exists(path string) (bool, error) {
12+
_, err := os.Stat(path)
13+
14+
if err == nil {
15+
return true, nil
16+
}
17+
18+
if os.IsNotExist(err) {
19+
return false, nil
20+
}
21+
22+
return false, err
23+
}
24+
25+
func LoadDevlocalConfig() (*config.DevlocalConfigYaml, error) {
26+
data, err := os.ReadFile(filepath.Join(config.PROJECT_ROOT, config.CONFIG_FILE_NAME))
27+
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
var cfg config.DevlocalConfigYaml
33+
34+
if err := yaml.Unmarshal(data, &cfg); err != nil {
35+
return nil, err
36+
}
37+
38+
return &cfg, nil
39+
}

internal/git/worktrees.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package git
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
)
7+
8+
func executeGitCommand(args []string) error {
9+
cmd := exec.Command("git", args...)
10+
11+
out, err := cmd.CombinedOutput()
12+
13+
if err != nil {
14+
return fmt.Errorf("[Error] Failed to run Skip Worktree: %s", out)
15+
}
16+
17+
return nil
18+
}
19+
20+
func SkipWorkTree(files []string) error {
21+
22+
filesToSkip := sanitizeToTrackedFiles(files)
23+
if len(filesToSkip) == 0 {
24+
return fmt.Errorf("[Error] No files listed in config are being tracked by git")
25+
}
26+
skipWorktreeArgs := append(
27+
[]string{
28+
"update-index",
29+
"--skip-worktree",
30+
},
31+
filesToSkip...,
32+
)
33+
return executeGitCommand(skipWorktreeArgs)
34+
}
35+
36+
func NoSkipWorkTree(files []string) error {
37+
38+
filesToRevert := sanitizeToTrackedFiles(files)
39+
if len(filesToRevert) == 0 {
40+
return fmt.Errorf("[Error] No files listed in config are being tracked by git")
41+
}
42+
skipWorktreeArgs := append(
43+
[]string{
44+
"update-index",
45+
"--no-skip-worktree",
46+
},
47+
filesToRevert...,
48+
)
49+
return executeGitCommand(skipWorktreeArgs)
50+
}
51+
52+
func isFileTracked(file string) bool {
53+
trackCheckArgs := []string{
54+
"ls-files",
55+
"--error-unmatch",
56+
file,
57+
}
58+
59+
err := executeGitCommand(trackCheckArgs)
60+
61+
return err == nil
62+
}
63+
64+
func sanitizeToTrackedFiles(files []string) []string {
65+
66+
filesTracked := []string{}
67+
68+
for _, file := range files {
69+
if isFileTracked(file) {
70+
filesTracked = append(filesTracked, file)
71+
}
72+
}
73+
74+
return filesTracked
75+
76+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package apply
2+
3+
import (
4+
"github.com/RohitRavindra-dev/devlocal/internal/filesystem"
5+
"github.com/RohitRavindra-dev/devlocal/internal/git"
6+
)
7+
8+
func Run() error {
9+
config, err := filesystem.LoadDevlocalConfig()
10+
11+
if err != nil {
12+
return err
13+
}
14+
15+
git.SkipWorkTree(config.Overlook)
16+
17+
return nil
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package apply
2+
3+
func overlook() {
4+
5+
}

0 commit comments

Comments
 (0)