wrote source code
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var installCmd = &cobra.Command{
|
||||
Use: "install",
|
||||
Short: "installs a package",
|
||||
Long: `installs a package from the aur to the system.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "error: package name required")
|
||||
return
|
||||
}
|
||||
pkg := args[0]
|
||||
url := "https://aur.archlinux.org/" + pkg
|
||||
|
||||
// git clone
|
||||
fmt.Println("cloning:", url)
|
||||
out, err := exec.Command("git", "clone", url).CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "git clone failed: %v\n%s\n", err, string(out))
|
||||
return
|
||||
}
|
||||
|
||||
// makepkg -si inside package dir
|
||||
fmt.Println("building/installing:", pkg)
|
||||
buildCmd := exec.Command("makepkg", "-si", "--noconfirm")
|
||||
buildCmd.Dir = pkg
|
||||
buildCmd.Stdin = os.Stdin
|
||||
buildCmd.Stdout = os.Stdout
|
||||
buildCmd.Stderr = os.Stderr
|
||||
buildCmd.Run()
|
||||
// cleanup
|
||||
if err := os.RemoveAll(pkg); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warning: failed to remove %s: %v\n", pkg, err)
|
||||
}
|
||||
|
||||
fmt.Println("install successful")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(installCmd)
|
||||
}
|
||||
|
||||
+2
-7
@@ -15,13 +15,8 @@ import (
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "apac",
|
||||
Short: "A brief description of your application",
|
||||
Long: `A longer description that spans multiple lines and likely contains
|
||||
examples and usage of using your application. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Short: "Simple arch aur helper.",
|
||||
Long: `Simple aur arch helper. It can manage packages for you`,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// uninstallCmd represents the uninstall command
|
||||
var uninstallCmd = &cobra.Command{
|
||||
Use: "uninstall",
|
||||
Short: "uninstalls a package",
|
||||
Long: `Uninstalls a package.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "error: package name required")
|
||||
return
|
||||
}
|
||||
pkg := args[0]
|
||||
|
||||
// Ensure running as root (pacman requires root)
|
||||
fmt.Println("uninstalling:", pkg)
|
||||
out := exec.Command("sudo", "pacman", "-R", pkg)
|
||||
out.Stdin =os.Stdin
|
||||
out.Stdout =os.Stdout
|
||||
out.Stderr =os.Stderr
|
||||
out.Run()
|
||||
|
||||
fmt.Println("uninstall successful")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(uninstallCmd)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user