Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Biniam Bekele committed Nov 12, 2021
1 parent dde55d6 commit 88617fd
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 94 deletions.
7 changes: 4 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/andornaut/gog/cmd/repositorycmd"
"github.com/andornaut/gog/internal/git"
"github.com/andornaut/gog/internal/link"
"github.com/andornaut/gog/internal/repository"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -41,7 +42,7 @@ var apply = &cobra.Command{
},
}

var git = &cobra.Command{
var git_ = &cobra.Command{
Use: "git [git command and arguments...]",
Short: "Run a git command in a repository's directory",
DisableFlagParsing: true,
Expand All @@ -52,7 +53,7 @@ var git = &cobra.Command{
if err != nil {
return err
}
return repository.GitRun(repoPath, args...)
return git.Run(repoPath, args...)
},
}

Expand Down Expand Up @@ -89,5 +90,5 @@ func init() {
apply.Flags().StringVarP(&repositoryFlag, "repository", "r", "", "name of repository")
remove.Flags().StringVarP(&repositoryFlag, "repository", "r", "", "name of repository")
Cmd.Flags().StringVarP(&repositoryFlag, "repository", "r", "", "name of repository")
Cmd.AddCommand(add, apply, git, remove, repositorycmd.Cmd)
Cmd.AddCommand(add, apply, git_, remove, repositorycmd.Cmd)
}
2 changes: 1 addition & 1 deletion cmd/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ func repoPath() (string, error) {
if err != nil {
return "", err
}
fmt.Println("REPOSITORY:", filepath.Base(repoPath))
fmt.Println("Repository:", filepath.Base(repoPath))
return repoPath, nil
}
45 changes: 11 additions & 34 deletions cmd/repositorycmd/repositorycmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package repositorycmd

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"

Expand Down Expand Up @@ -32,25 +30,11 @@ var add = &cobra.Command{
repoURL = args[1]
}

if err := repository.ValidateRepoName(repoName); err != nil {
return err
}

repoPath := path.Join(repository.BaseDir, repoName)
if err := os.MkdirAll(repoPath, 0755); err != nil {
repoPath, err := repository.Add(repoName, repoURL)
if err != nil {
return err
}

if repoURL == "" {
if err := repository.GitInit(repoPath); err != nil {
return err
}
} else {
if err := repository.GitClone(repoPath, repoURL); err != nil {
return err
}
}
fmt.Println(repoPath)
fmt.Printf("Added repository: %s\n", repoPath)
return nil
},
}
Expand Down Expand Up @@ -82,19 +66,15 @@ var list = &cobra.Command{
Args: cobra.NoArgs,
DisableFlagsInUseLine: true,
RunE: func(c *cobra.Command, args []string) error {
entries, err := ioutil.ReadDir(repository.BaseDir)
names, err := repository.List()
if err != nil {
return err
}

for _, fileInfo := range entries {
if fileInfo.IsDir() {
msg := fileInfo.Name()
if isPath {
msg = path.Join(repository.BaseDir, msg)
}
fmt.Println(msg)
for _, msg := range names {
if isPath {
msg = path.Join(repository.BaseDir, msg)
}
fmt.Println(msg)
}
return nil
},
Expand All @@ -107,14 +87,11 @@ var remove = &cobra.Command{
DisableFlagsInUseLine: true,
RunE: func(c *cobra.Command, args []string) error {
repoName := args[0]
if err := repository.ValidateRepoName(repoName); err != nil {
return err
}
repoPath := path.Join(repository.BaseDir, repoName)
if err := os.RemoveAll(repoPath); err != nil {
repoPath, err := repository.Remove(repoName)
if err != nil {
return err
}
fmt.Println(repoPath)
fmt.Printf("Removed repository: %s\n", repoPath)
return nil
},
}
Expand Down
34 changes: 34 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package git

import (
"os"
"os/exec"
)

// GitClone clones a git repostory
func Clone(baseDir, repoPath string, repoURL string) error {
return Run(baseDir, "clone", repoURL, repoPath)
}

// GitInit initializes a git repository
func Init(baseDir, repoPath string) error {
return Run(baseDir, "init", repoPath)
}

// Is returns true if the given directory is a git repository
func Is(baseDir string) bool {
cmd := exec.Command("git", "rev-parse", "--git-dir")
cmd.Dir = baseDir
err := cmd.Run()
return err == nil
}

// GitRun runs a git command in a repository
func Run(baseDir string, arguments ...string) error {
cmd := exec.Command("git", arguments...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = baseDir
return cmd.Run()
}
30 changes: 28 additions & 2 deletions internal/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,38 @@ var (

// Unlink unlinks the given paths
func Unlink(repoPath string, paths []string) error {
return repository.SyncLinks(repoPath, paths, UnlinkDir, UnlinkFile)
return syncLinks(repoPath, paths, UnlinkDir, UnlinkFile)
}

// Link unlinks the given paths
func Link(repoPath string, paths []string) error {
return repository.SyncLinks(repoPath, paths, Dir, File)
return syncLinks(repoPath, paths, Dir, File)
}

type syncFunc func(string, string) error

func syncLinks(repoPath string, paths []string, updateDir, updateFile syncFunc) error {
for _, extPath := range paths {
intPath := repository.ToInternalPath(repoPath, extPath)
intFileInfo, err := os.Lstat(intPath)
if err != nil {
if os.IsNotExist(err) {
// Nothing to update
continue
}
return err
}
if intFileInfo.IsDir() {
if err := updateDir(repoPath, intPath); err != nil {
return err
}
continue
}
if err := updateFile(repoPath, intPath); err != nil {
return err
}
}
return nil
}

// Dir recursively creates symbolic links from a repository directory's files
Expand Down
26 changes: 0 additions & 26 deletions internal/repository/git.go

This file was deleted.

62 changes: 41 additions & 21 deletions internal/repository/sync.go → internal/repository/manage.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,55 @@
package repository

import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/andornaut/gog/internal/copy"
"github.com/andornaut/gog/internal/git"
)

type syncFunc func(string, string) error
// Add adds a new repository
func Add(repoName, repoURL string) (string, error) {
if err := validateRepoName(repoName); err != nil {
return "", err
}

// SyncLinks synchronizes all given paths within `repoPath`
func SyncLinks(repoPath string, paths []string, updateDir, updateFile syncFunc) error {
for _, extPath := range paths {
intPath := ToInternalPath(repoPath, extPath)
intFileInfo, err := os.Lstat(intPath)
if err != nil {
if os.IsNotExist(err) {
// Nothing to update
continue
}
return err
}
if intFileInfo.IsDir() {
if err := updateDir(repoPath, intPath); err != nil {
return err
}
continue
repoPath := path.Join(BaseDir, repoName)
if err := validateRepoPath(repoPath); err == nil {
return "", fmt.Errorf("repository already exists: %s", repoPath)
}

if err := os.MkdirAll(repoPath, 0755); err != nil {
return "", err
}

if repoURL == "" {
if err := git.Init(BaseDir, repoPath); err != nil {
return "", err
}
if err := updateFile(repoPath, intPath); err != nil {
return err
} else {
if err := git.Clone(BaseDir, repoPath, repoURL); err != nil {
return "", err
}
}
return nil
return repoPath, nil
}

// Remove removes an existing repository
func Remove(repoName string) (string, error) {
if err := validateRepoName(repoName); err != nil {
return "", err
}
repoPath := path.Join(BaseDir, repoName)
if err := validateRepoPath(repoPath); err != nil {
return "", err
}
if err := os.RemoveAll(repoPath); err != nil {
return "", err
}
return repoPath, nil
}

// AddPaths adds the given paths from the given repository
Expand Down Expand Up @@ -82,6 +100,8 @@ func removePath(repoPath, targetPath string) error {
return os.RemoveAll(intPath)
}

type syncFunc func(string, string) error

// syncRepository synchronizes all given paths within `repoPath`
func syncRepository(repoPath string, paths []string, updateRepository syncFunc) error {
for _, extPath := range paths {
Expand Down
23 changes: 22 additions & 1 deletion internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,35 @@ func GetDefault() (string, error) {
return getFirst()
}

// List returns a list of repositories
func List() ([]string, error) {
entries, err := ioutil.ReadDir(BaseDir)
if err != nil {
return nil, err
}
repoNames := []string{}
for _, fileInfo := range entries {
repoName := fileInfo.Name()
if err := validateRepoName(repoName); err != nil {
continue
}
repoPath := path.Join(BaseDir, repoName)
if err := validateRepoPath(repoPath); err != nil {
continue
}
repoNames = append(repoNames, repoName)
}
return repoNames, nil
}

// RootPath returns an absolute filesystem path which corresponds to the given
// repository name or the default repository's path if the given name is empty
func RootPath(name string) (string, error) {
if name == "" {
return GetDefault()
}

if err := ValidateRepoName(name); err != nil {
if err := validateRepoName(name); err != nil {
return "", err
}
p := path.Join(BaseDir, name)
Expand Down
17 changes: 11 additions & 6 deletions internal/repository/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,36 @@ import (
"os"
"regexp"
"strings"

"github.com/andornaut/gog/internal/git"
)

// ValidateRepoName returns an error if the repo name is invalid
func ValidateRepoName(name string) error {
// validateRepoName returns an error if the repo name is invalid
func validateRepoName(name string) error {
validRepoName := regexp.MustCompile(`^[\w-_]+$`)
if !validRepoName.MatchString(name) {
return fmt.Errorf("Invalid repository name: %s", name)
return fmt.Errorf("invalid repository name: %s", name)
}
return nil
}

func validateRepoPath(p string) error {
fileInfo, err := os.Stat(p)
if err != nil {
return fmt.Errorf("Invalid repository path: %s", p)
return fmt.Errorf("invalid repository path: %s", p)
}
if !fileInfo.IsDir() {
return fmt.Errorf("Repository path must be a directory: %s", p)
return fmt.Errorf("repository path must be a directory: %s", p)
}
if !git.Is(p) {
return fmt.Errorf("repository must be initialized as a git repository")
}
return nil
}

func validateTargetPath(p string) error {
if shouldSkip(p, "") {
return fmt.Errorf("Invalid target path: %s", p)
return fmt.Errorf("invalid target path: %s", p)
}
return nil
}
Expand Down

0 comments on commit 88617fd

Please sign in to comment.