-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert from shell script to yml (#15)
- Loading branch information
Showing
13 changed files
with
232 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.