yay/yay.go

101 lines
1.7 KiB
Go
Raw Normal View History

2016-09-05 02:17:51 +00:00
package main
import (
2016-10-02 20:50:23 +00:00
"bytes"
"errors"
2016-09-05 02:17:51 +00:00
"fmt"
"os"
"strings"
)
2016-09-13 01:06:24 +00:00
// PacmanConf describes the default pacman config file
const PacmanConf string = "/etc/pacman.conf"
2016-09-06 21:22:20 +00:00
2016-09-05 22:47:36 +00:00
// BuildDir is the root for package building
2016-09-06 21:22:20 +00:00
const BuildDir string = "/tmp/yaytmp/"
2016-09-05 22:47:36 +00:00
// SearchMode is search without numbers.
const SearchMode int = -1
func operation() (operation string, err error) {
if len(os.Args) < 2 {
return "noop", errors.New("No operation specified.")
}
for _, arg := range os.Args[1:] {
if arg[0] == '-' && arg[1] != '-' {
return arg, nil
}
}
return "yogurt", nil
}
2016-10-02 20:50:23 +00:00
func packages() ([]string, error) {
var ps []string
for _, arg := range os.Args[1:] {
if arg[0] != '-' {
ps = append(ps, arg)
}
}
2016-10-02 20:50:23 +00:00
return ps, nil
}
func flags() (flags string, err error) {
var fs []string
for _, arg := range os.Args[1:] {
if arg[0] == '-' && arg[1] == '-' {
fs = append(fs, arg)
}
}
if len(fs) == 0 {
return "", nil
}
flags = strings.Join(fs, " ")
return
}
2016-09-05 02:17:51 +00:00
func main() {
2016-09-09 13:55:16 +00:00
var err error
2016-10-02 20:50:23 +00:00
var pkgstring bytes.Buffer
2016-09-13 01:06:24 +00:00
conf, err := readConfig(PacmanConf)
op, err := operation()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2016-10-02 20:50:23 +00:00
pkgs, _ := packages()
flag, _ := flags()
2016-09-10 23:02:53 +00:00
switch op {
case "-Ss":
2016-10-02 20:50:23 +00:00
for _, pkg := range pkgs {
err = searchMode(pkg, &conf)
}
case "-S":
2016-10-02 20:50:23 +00:00
err = InstallPackage(pkgs, &conf, flag)
case "-Syu":
err = updateAndInstall(&conf, flag)
case "yogurt":
2016-10-02 20:50:23 +00:00
for _, pkg := range pkgs {
err = searchAndInstall(pkg, &conf, flag)
}
default:
2016-10-02 20:50:23 +00:00
for i, pkg := range pkgs {
pkgstring.WriteString(pkg)
if i != len(pkgs)-1 {
pkgstring.WriteString(" ")
}
}
err = passToPacman(op, pkgstring.String(), flag)
2016-09-09 13:55:16 +00:00
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
2016-09-05 02:17:51 +00:00
}