Skip to content

Commit

Permalink
convert from shell script to yml (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoriqq authored Jul 18, 2022
1 parent c5fed70 commit 3964b69
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 155 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.18

require (
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/fatih/color v1.13.0
github.com/go-git/go-git/v5 v5.4.2
github.com/goccy/go-yaml v1.9.5
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.0
)
Expand Down Expand Up @@ -40,9 +42,10 @@ require (
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
54 changes: 26 additions & 28 deletions go.sum

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions internal/cmd/aquifer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"github.com/anoriqq/qpm/internal/service/aquifer"
"github.com/spf13/cobra"
)

var aquiferCmd = &cobra.Command{
Use: "aquifer",
Short: "manage aquifer",
}

func init() {
rootCmd.AddCommand(aquiferCmd)
}

var aquiferPullCmd = &cobra.Command{
Use: "pull",
Short: "get aquifer form remote repository",
RunE: aquiferPullRun,
}

func init() {
aquiferCmd.AddCommand(aquiferPullCmd)
}

func aquiferPullRun(_ *cobra.Command, _ []string) error {
return aquifer.Pull()
}
4 changes: 2 additions & 2 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
var configCmd = &cobra.Command{
Use: "config",
Short: "update config",
Example: ` # set ScriptDir to ~/.qpm
qpm config ScriptDir ~/.qpm`,
Example: ` # set AquiferDir to ~/.qpm
qpm config AquiferDir ~/.qpm`,
RunE: configRun,
}

Expand Down
20 changes: 0 additions & 20 deletions internal/cmd/getscript.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"errors"

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

Expand All @@ -23,7 +23,7 @@ func installRun(_ *cobra.Command, args []string) error {
return err
}

return install.Install(pkgName)
return aquifer.Install(pkgName)
}

func getPkgName(args []string) (string, error) {
Expand Down
20 changes: 10 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func createIfNotExistConfigFile(configHome, configName, configType string) error
var Cfg Config

type Config struct {
ScriptDir string
ScriptRepoURL string
AquiferDir string
AquiferRepoURL string
GitHubUsername string
GitHubAccessToken string
}
Expand All @@ -83,12 +83,12 @@ func Load() error {
return nil
}

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

func SetScriptDir(scriptDir string) error {
viper.Set("ScriptDir", scriptDir)
func SetAquiferDir(aquiferDir string) error {
viper.Set("AquiferDir", aquiferDir)

err := viper.WriteConfig()
if err != nil {
Expand All @@ -98,12 +98,12 @@ func SetScriptDir(scriptDir string) error {
return viper.Unmarshal(&Cfg)
}

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

func SetScriptRepoURL(scriptRepoURL string) error {
viper.Set("ScriptRepoURL", scriptRepoURL)
func SetAquiferRepoURL(aquiferRepoURL string) error {
viper.Set("AquiferRepoURL", aquiferRepoURL)

err := viper.WriteConfig()
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions internal/config/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ func SetCfgWithSurvey(setters ...func() error) error {
return nil
}

func SetScriptDirWithSurvey() error {
if hasScriptDir() {
func SetAquiferDirWithSurvey() error {
if hasAquiferDir() {
return nil
}

scriptDir, err := stdin.SurveyScriptDir()
aquiferDir, err := stdin.SurveyAquiferDir()
if err != nil {
return err
}

err = SetScriptDir(scriptDir)
err = SetAquiferDir(aquiferDir)
if err != nil {
return err
}

return nil
}

func SetScriptRepoURLWithSurvey() error {
if hasScriptRepoURL() {
func SetAquiferRepoURLWithSurvey() error {
if hasAquiferRepoURL() {
return nil
}

scriptRepoURL, err := stdin.SurveyScriptRepoURL()
aquiferRepoURL, err := stdin.SurveyAquiferRepoURL()
if err != nil {
return err
}

err = SetScriptRepoURL(scriptRepoURL.String())
err = SetAquiferRepoURL(aquiferRepoURL.String())
if err != nil {
return err
}
Expand Down
124 changes: 124 additions & 0 deletions internal/service/aquifer/aquifer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package aquifer

import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/anoriqq/qpm/internal/config"
"github.com/fatih/color"
"github.com/goccy/go-yaml"
)

const (
envOS = "QPM_OS"
envArch = "QPM_ARCH"
)

var headerOutput = color.New(color.FgHiCyan).Add(color.Bold)

type Plan struct {
Dependencies []string
Run []string
}

type Aquifer struct {
Version string
Name string
Install map[string]Plan
Uninstall map[string]Plan
}

func Install(pkgName string) error {
err := config.SetCfgWithSurvey(
config.SetAquiferDirWithSurvey,
)
if err != nil {
return err
}

aquiferPath, err := getAquiferPath(config.Cfg.AquiferDir, pkgName)
if err != nil {
return err
}

aquifer, err := getAquifer(aquiferPath)
if err != nil {
return err
}

plan, err := getPlan(aquifer, runtime.GOOS)
if err != nil {
return err
}

return installAquifer(plan)
}

func getAquiferPath(aquiferDir, pkgName string) (string, error) {
aquiferPath, err := filepath.Abs(fmt.Sprintf("%s/%s/latest.yml", aquiferDir, pkgName))
if err != nil {
return "", err
}

return aquiferPath, nil
}

func getAquifer(aquiferPath string) (Aquifer, error) {
_, err := os.Stat(aquiferPath)
if os.IsNotExist(err) {
return Aquifer{}, fmt.Errorf("aquifer not found: %s", aquiferPath)
}

f, err := os.Open(aquiferPath)
if err != nil {
return Aquifer{}, err
}
defer f.Close()

bytes, err := io.ReadAll(f)
if err != nil {
return Aquifer{}, err
}

var aquifer Aquifer
err = yaml.Unmarshal(bytes, &aquifer)
if err != nil {
return Aquifer{}, err
}

return aquifer, nil
}

func getPlan(aquifer Aquifer, os string) (Plan, error) {
plan, ok := aquifer.Install[os]
if !ok {
return Plan{}, fmt.Errorf("not declared os: %s", runtime.GOOS)
}

return plan, nil
}

func installAquifer(plan Plan) error {
for i, v := range plan.Run {
headerOutput.Printf("[%d] %s\n", i+1, v)

c := exec.Command("bash", "-c", v)
c.Env = append(c.Env,
fmt.Sprintf("%s=%s", envOS, runtime.GOARCH),
fmt.Sprintf("%s=%s", envArch, runtime.GOOS),
)

output, err := c.Output()
if err != nil {
return err
}

fmt.Printf("%v", string(output))
}

return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package getscript
package aquifer

import (
"fmt"
Expand All @@ -10,23 +10,26 @@ import (
"github.com/anoriqq/qpm/internal/stdin"
)

func GetScript() error {
var fileFmt = time.Now().Format("20060102150405")

func Pull() error {
err := config.SetCfgWithSurvey(
config.SetScriptDirWithSurvey,
config.SetScriptRepoURLWithSurvey,
config.SetAquiferDirWithSurvey,
config.SetAquiferRepoURLWithSurvey,
config.SetGitHubUsernameWithSurvey,
config.SetGitHubAccessTokenWithSurvey,
)
if err != nil {
return err
}

err = stdin.SurveyDeleteScriptDir()
err = stdin.SurveyDeleteAquiferDir()
if err != nil {
return err
}

err = os.Rename(config.Cfg.ScriptDir, fmt.Sprintf("%s.old_%s", config.Cfg.ScriptDir, time.Now().Format("20060102150405")))
oldName := fmt.Sprintf("%s.old_%s", config.Cfg.AquiferDir, fileFmt)
err = os.Rename(config.Cfg.AquiferDir, oldName)
if err != nil && !os.IsNotExist(err) {
return err
}
Expand All @@ -36,9 +39,9 @@ func GetScript() error {
return err
}

fmt.Println(config.Cfg.ScriptRepoURL)
fmt.Println(config.Cfg.AquiferRepoURL)

err = c.Clone(config.Cfg.ScriptDir, config.Cfg.ScriptRepoURL)
err = c.Clone(config.Cfg.AquiferDir, config.Cfg.AquiferRepoURL)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/service/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func PrintConfig() {

func SetConfig(configField, configValue string) error {
switch strings.ToLower(configField) {
case "scriptdir":
config.SetScriptDir(configValue)
case "aquiferdir":
config.SetAquiferDir(configValue)
return nil
case "scriptrepourl":
config.SetScriptRepoURL(configValue)
case "aquiferrepourl":
config.SetAquiferRepoURL(configValue)
return nil
case "githubusername":
config.SetGitHubUsername(configValue)
Expand Down
Loading

0 comments on commit 3964b69

Please sign in to comment.