Skip to content

Commit

Permalink
refactor: split from cmd method to service or domain func (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoriqq authored Jul 17, 2022
1 parent e08c8b9 commit 0551a4b
Show file tree
Hide file tree
Showing 11 changed files with 385 additions and 255 deletions.
49 changes: 17 additions & 32 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package cmd

import (
"errors"
"fmt"
"strings"

"github.com/anoriqq/qpm/internal/config"
"github.com/anoriqq/qpm/internal/service/config"
"github.com/spf13/cobra"
)

Expand All @@ -21,46 +19,33 @@ func init() {
rootCmd.AddCommand(configCmd)
}

var jsonToStringReplacer = strings.NewReplacer("{", "", "}", "")

func configRun(_ *cobra.Command, args []string) error {
if len(args) == 0 {
printConfig()
config.PrintConfig()
return nil
}

if len(args) < 2 {
return errors.New("config field and value is required")
configField, configValue, err := getConfigInput(args)
if err != nil {
return err
}
if len(args) != 2 {
return errors.New("too many arguments")
}

configField, configValue := args[0], args[1]

switch strings.ToLower(configField) {
case "scriptdir":
config.SetScriptDir(configValue)
case "scriptrepourl":
config.SetScriptRepoURL(configValue)
case "githubusername":
config.SetGitHubUsername(configValue)
case "githubaccesstoken":
config.SetGitHubAccessToken(configValue)
default:
return fmt.Errorf("unknown config field: %s", configField)
err = config.SetConfig(configField, configValue)
if err != nil {
return err
}

printConfig()

config.PrintConfig()
return nil
}

func printConfig() {
cfgText := fmt.Sprintf("%+v\n", config.Cfg)
cfgTexts := strings.Split(cfgText, " ")
for _, t := range cfgTexts {
text := strings.Replace(jsonToStringReplacer.Replace(t), ":", ": ", 1)
fmt.Println(text)
func getConfigInput(args []string) (field, value string, err error) {
if len(args) < 2 {
return "", "", errors.New("config field and value is required")
}
if len(args) > 2 {
return "", "", errors.New("too many arguments")
}

return args[0], args[1], nil
}
144 changes: 2 additions & 142 deletions internal/cmd/getscript.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package cmd

import (
"errors"
"fmt"
"net/url"
"os"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/anoriqq/qpm/internal/config"
"github.com/anoriqq/qpm/internal/git"
"github.com/anoriqq/qpm/internal/service/getscript"
"github.com/spf13/cobra"
)

Expand All @@ -24,137 +16,5 @@ func init() {
}

func getscriptRun(_ *cobra.Command, _ []string) error {
if !config.HasScriptDir() {
scriptDir, err := surveyScriptDir()
if err != nil {
return err
}

err = config.SetScriptDir(scriptDir)
if err != nil {
return err
}
}

if !config.HasScriptRepoURL() {
scriptRepoURL, err := surveyScriptRepoURL()
if err != nil {
return err
}

err = config.SetScriptRepoURL(scriptRepoURL.String())
if err != nil {
return err
}
}

if !config.HasGitHubUsername() {
githubUsername, err := surveyGitHubUsername()
if err != nil {
return err
}

err = config.SetGitHubUsername(githubUsername)
if err != nil {
return err
}
}

if !config.HasGitHubAccessToken() {
githubAccessToken, err := surveyGitHubAccessToken()
if err != nil {
return err
}

err = config.SetGitHubAccessToken(githubAccessToken)
if err != nil {
return err
}
}

err := surveyDeleteScriptDir()
if err != nil {
return err
}

err = os.Rename(config.Cfg.ScriptDir, fmt.Sprintf("%s.old_%s", config.Cfg.ScriptDir, time.Now().Format("20060102150405")))
if err != nil && !os.IsNotExist(err) {
return err
}

c, err := git.NewClient(config.Cfg.GitHubUsername, config.Cfg.GitHubAccessToken)
if err != nil {
return err
}

fmt.Println(config.Cfg.ScriptRepoURL)

err = c.Clone(config.Cfg.ScriptDir, config.Cfg.ScriptRepoURL)
if err != nil {
return err
}

return nil
}

func surveyScriptRepoURL() (*url.URL, error) {
var rawScriptRepoURL string
p := &survey.Input{
Message: "Please enter qpm script repository full URL.",
}
err := survey.AskOne(p, &rawScriptRepoURL, survey.WithValidator(survey.Required))
if err != nil {
return nil, err
}

scriptRepoURL, err := url.Parse(rawScriptRepoURL)
if err != nil {
return nil, err
}

return scriptRepoURL, nil
}

func surveyDeleteScriptDir() error {
var canDelete bool
p := &survey.Confirm{
Message: "Do you want to replace the script dir and continue?",
Default: false,
}
err := survey.AskOne(p, &canDelete)
if err != nil {
return err
}

if !canDelete {
return errors.New("To clone the script repository, the script dir must be deleted.")
}

return nil
}

func surveyGitHubUsername() (string, error) {
var githubUsername string
p := &survey.Input{
Message: "Please enter github username.",
}
err := survey.AskOne(p, &githubUsername, survey.WithValidator(survey.Required))
if err != nil {
return "", err
}

return githubUsername, nil
}

func surveyGitHubAccessToken() (string, error) {
var githubAccessToken string
p := &survey.Password{
Message: "Please enter github access token.",
}
err := survey.AskOne(p, &githubAccessToken, survey.WithValidator(survey.Required))
if err != nil {
return "", err
}

return githubAccessToken, nil
return getscript.GetScript()
}
79 changes: 2 additions & 77 deletions internal/cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package cmd

import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/AlecAivazis/survey/v2"
"github.com/anoriqq/qpm/internal/config"
"github.com/anoriqq/qpm/internal/service/install"
"github.com/spf13/cobra"
)

Expand All @@ -25,48 +18,12 @@ func init() {
}

func installRun(_ *cobra.Command, args []string) error {
if !config.HasScriptDir() {
scriptDir, err := surveyScriptDir()
if err != nil {
return err
}

err = config.SetScriptDir(scriptDir)
if err != nil {
return err
}
}

pkgName, err := getPkgName(args)
if err != nil {
return err
}

installScriptPath, err := getInstallScriptPaht(config.Cfg.ScriptDir, pkgName)
if err != nil {
return err
}

return execInstallScript(installScriptPath)
}

func surveyScriptDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}

var scriptDir string
p := &survey.Input{
Message: "Please enter qpm script path.",
Default: filepath.Join(home, ".qpm"),
}
err = survey.AskOne(p, &scriptDir, survey.WithValidator(survey.Required))
if err != nil {
return "", err
}

return scriptDir, nil
return install.Install(pkgName)
}

func getPkgName(args []string) (string, error) {
Expand All @@ -76,35 +33,3 @@ func getPkgName(args []string) (string, error) {

return args[0], nil
}

func getInstallScriptPaht(scriptDir, pkgName string) (string, error) {
installScriptPath, err := filepath.Abs(fmt.Sprintf("%s/%s/latest.sh", scriptDir, pkgName))
if err != nil {
return "", err
}

return installScriptPath, nil
}

func execInstallScript(installScriptPath string) error {
_, err := os.Stat(installScriptPath)
if os.IsNotExist(err) {
return fmt.Errorf("install script not found: %s", installScriptPath)
}

c := exec.Command("/bin/sh", installScriptPath, "install", runtime.GOOS, runtime.GOARCH)

stdout, err := c.StdoutPipe()
if err != nil {
return err
}

c.Start()

scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
fmt.Println(scanner.Text())
}

return c.Wait()
}
8 changes: 4 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Load() error {
return nil
}

func HasScriptDir() bool {
func hasScriptDir() bool {
return len(Cfg.ScriptDir) != 0
}

Expand All @@ -98,7 +98,7 @@ func SetScriptDir(scriptDir string) error {
return viper.Unmarshal(&Cfg)
}

func HasScriptRepoURL() bool {
func hasScriptRepoURL() bool {
return len(Cfg.ScriptRepoURL) != 0
}

Expand All @@ -113,7 +113,7 @@ func SetScriptRepoURL(scriptRepoURL string) error {
return viper.Unmarshal(&Cfg)
}

func HasGitHubUsername() bool {
func hasGitHubUsername() bool {
return len(Cfg.GitHubUsername) != 0
}

Expand All @@ -128,7 +128,7 @@ func SetGitHubUsername(githubUsername string) error {
return viper.Unmarshal(&Cfg)
}

func HasGitHubAccessToken() bool {
func hasGitHubAccessToken() bool {
return len(Cfg.GitHubAccessToken) != 0
}

Expand Down
Loading

0 comments on commit 0551a4b

Please sign in to comment.