feat(cli): add init sub-command

This commit is contained in:
DataHearth 2022-02-24 22:38:17 +01:00
parent 4e95bd1719
commit 8f49f3df36
4 changed files with 145 additions and 58 deletions

78
cmd/cli.go Normal file
View File

@ -0,0 +1,78 @@
package cmd
import (
"fmt"
"log"
"os"
"github.com/datahearth/config-mapper/internal/git"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
errLogger = log.New(os.Stderr, "", 0)
logger = log.New(os.Stderr, "", 0)
)
var rootCmd = &cobra.Command{
Use: "config-mapper",
Short: "Manage your systems configuration",
Long: `config-mapper aims to help you manage your configurations between systems
with a single configuration file.`,
}
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize your configuration folder",
Long: `Initialize will retrieve your configuration folder from the source location and
copy it into the destination field`,
Run: func(cmd *cobra.Command, args []string) {
logger.Println("initializing config-mapper folder from configuration...")
if _, err := git.OpenGitRepo(); err != nil {
errLogger.Printf("failed to initialize folder: %v\n", err)
os.Exit(1)
}
logger.Printf("repository initialized at \"%v\"\n", viper.GetString("storage.location"))
},
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.AddCommand(initCmd)
}
func initConfig() {
h, err := os.UserHomeDir()
if err != nil {
errLogger.Printf("can't get home directory through $HOME variable: %v\n", err)
os.Exit(1)
}
if c := os.Getenv("CONFIG_MAPPER_CFG"); c != "" {
viper.AddConfigPath(c)
} else {
viper.AddConfigPath(h)
}
viper.SetConfigType("yml")
viper.SetConfigName("config-mapper")
viper.SetDefault("storage.location", fmt.Sprintf("%s/config-mapper", os.TempDir()))
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
errLogger.Println("configuration file not found", err)
} else {
errLogger.Printf("failed to read config: %v\n", err)
}
os.Exit(1)
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
errLogger.Printf("an error occured while running command: %v\n", err)
os.Exit(1)
}
}

View File

@ -1,53 +0,0 @@
package cmd
import (
"log"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var errLogger = log.New(os.Stderr, "", 0)
var rootCmd = &cobra.Command{
Use: "config-mapper",
Short: "Manage your systems configuration",
Long: `config-mapper aims to help you manage your configurations between systems
with a single configuration file.`,
Run: func(cmd *cobra.Command, args []string) {
},
}
func init() {
cobra.OnInitialize(initConfig)
}
func initConfig() {
h, err := os.UserHomeDir()
if err != nil {
errLogger.Printf("can't get home directory through $HOME variable: %v\n", err)
os.Exit(1)
}
if c := os.Getenv("CONFIG_MAPPER"); c != "" {
viper.AddConfigPath(c)
} else {
viper.AddConfigPath(h)
}
viper.SetConfigType("yml")
viper.SetConfigName("config-mapper")
if err := viper.ReadInConfig(); err != nil {
errLogger.Printf("failed to read config: %v\n", err)
os.Exit(1)
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
errLogger.Printf("an error occured while running command: %v\n", err)
os.Exit(1)
}
}

View File

@ -9,11 +9,13 @@ type Configuration struct {
type Storage struct {
Location string `yaml:"location"`
Git struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Repository string `yaml:"repository"`
} `yaml:"git"`
Git Git `yaml:"git"`
}
type Git struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Repository string `yaml:"repository"`
}
type PkgManagers struct {

60
internal/git/git.go Normal file
View File

@ -0,0 +1,60 @@
package git
import (
"errors"
"os"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/spf13/viper"
)
var (
ErrDirIsFile = errors.New("path is a file")
ErrEmptyGitConfig = errors.New("empty git configuration")
)
func OpenGitRepo() (*git.Repository, error) {
configFolder := viper.GetString("storage.location")
s, err := os.Stat(configFolder)
if err != nil {
if os.IsNotExist(err) {
gitConfig := viper.GetStringMapString("storage.git")
if gitConfig == nil {
return nil, ErrEmptyGitConfig
}
repo, err := git.PlainClone(viper.GetString("storage.location"), false, &git.CloneOptions{
URL: gitConfig["repository"],
Progress: os.Stdout,
Auth: &http.BasicAuth{
Username: gitConfig["username"],
Password: gitConfig["password"],
},
})
if err != nil {
return nil, err
}
return repo, nil
}
return nil, err
}
if s.IsDir() {
return nil, ErrDirIsFile
}
repo, err := git.PlainOpen(configFolder)
if err != nil {
if err == git.ErrRepositoryNotExists {
return nil, err
}
return nil, err
}
return repo, nil
}