Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
GiGurra committed Jul 13, 2023
1 parent 04d99b9 commit a586fe1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
93 changes: 93 additions & 0 deletions cmd/repos/repos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package repos

import (
"context"
"encoding/json"
"fmt"
"github.com/gigurra/flycd/internal/flycd"
"github.com/gigurra/flycd/internal/flycd/model"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

var flags struct {
force *bool
}

var Cmd = &cobra.Command{
Use: "repos <path>",
Short: "Traverse the project structure and list all git repos referenced. Useful for finding your dependencies (and setting up webhooks).",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
argPath := args[0]
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error getting current working directory: %v\n", err)
os.Exit(1)
}

path := func() string {
if filepath.IsAbs(argPath) {
return argPath
} else {
return filepath.Join(cwd, argPath)
}
}()

fmt.Printf("Scanning for git repos referenced inside project @ %s\n", path)

appRepos := make([]model.AppNode, 0)
projectRepos := make([]model.ProjectNode, 0)

ctx := context.Background()
err = flycd.TraverseDeepAppTree(ctx, path, model.TraverseAppTreeOptions{
ValidAppCb: func(node model.AppNode) error {
if node.AppConfig.Source.Repo != "" {
appRepos = append(appRepos, node)
}
return nil
},
BeginProjectCb: func(node model.ProjectNode) error {
if node.IsValidProject() {
if node.ProjectConfig.Source.Repo != "" {
projectRepos = append(projectRepos, node)
}
} else {
fmt.Printf("Skipping project (invalid) %s @ %s\n", node.ProjectConfig.Project, node.Path)
}
return nil
},
})

fmt.Printf("Found the following project repo references:\n")
for _, node := range projectRepos {
srcJson, err := json.Marshal(node.ProjectConfig.Source)
if err != nil {
fmt.Printf("Error marshalling source config: %v\n", err)
os.Exit(1)
}
fmt.Printf(" - %s @ %s\n", node.ProjectConfig.Project, srcJson)
}

fmt.Printf("Found the following app repo references:\n")
for _, node := range appRepos {
srcJson, err := json.Marshal(node.AppConfig.Source)
if err != nil {
fmt.Printf("Error marshalling source config: %v\n", err)
os.Exit(1)
}
fmt.Printf(" - %s @ %s\n", node.AppConfig.App, srcJson)
}

if err != nil {
fmt.Printf("Error walking path %s: %v\n", path, err)
os.Exit(1)
}

},
}

func init() {
flags.force = Cmd.Flags().BoolP("force", "f", false, "Force overwrite of existing files")
}
16 changes: 8 additions & 8 deletions internal/flycd/model/source_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package model
import "fmt"

type GitRef struct {
Branch string `yaml:"branch,omitempty" toml:"branch"`
Tag string `yaml:"tag,omitempty" toml:"tag"`
Commit string `yaml:"commit,omitempty" toml:"commit"`
Branch string `yaml:"branch,omitempty" toml:"branch" json:"branch,omitempty"`
Tag string `yaml:"tag,omitempty" toml:"tag" json:"tag,omitempty"`
Commit string `yaml:"commit,omitempty" toml:"commit" json:"commit,omitempty"`
}

func (g *GitRef) IsEmpty() bool {
return g.Branch == "" && g.Tag == "" && g.Commit == ""
}

type Source struct {
Repo string `yaml:"repo,omitempty" toml:"repo"`
Path string `yaml:"path,omitempty" toml:"path"`
Ref GitRef `yaml:"ref,omitempty" toml:"ref"`
Type SourceType `yaml:"type,omitempty" toml:"type"`
Inline string `yaml:"inline,omitempty" toml:"inline"`
Repo string `yaml:"repo,omitempty" toml:"repo" json:"repo,omitempty"`
Path string `yaml:"path,omitempty" toml:"path" json:"path,omitempty"`
Ref GitRef `yaml:"ref,omitempty" toml:"ref" json:"ref,omitempty"`
Type SourceType `yaml:"type,omitempty" toml:"type" json:"type,omitempty"`
Inline string `yaml:"inline,omitempty" toml:"inline" json:"inline,omitempty"`
}

func NewInlineDockerFileSource(inline string) Source {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/gigurra/flycd/cmd/deploy"
"github.com/gigurra/flycd/cmd/install"
"github.com/gigurra/flycd/cmd/monitor"
"github.com/gigurra/flycd/cmd/repos"
"github.com/gigurra/flycd/internal/flycd"
"github.com/spf13/cobra"
"os"
Expand Down Expand Up @@ -60,6 +61,7 @@ func main() {
monitor.Cmd(deployService, webhookService),
install.Cmd(PackagedFileSystem, deployService),
convert.Cmd,
repos.Cmd,
)

// run cli
Expand Down

0 comments on commit a586fe1

Please sign in to comment.