stroy.go raw

   1  // +build !windows
   2  
   3  package main
   4  
   5  import (
   6  	"fmt"
   7  	"io/ioutil"
   8  	"os"
   9  	"os/exec"
  10  	"path/filepath"
  11  	"strings"
  12  
  13  	_ "github.com/p9c/p9/pkg/fork"
  14  
  15  	"github.com/p9c/p9/pkg/appdata"
  16  	"github.com/p9c/p9/pkg/apputil"
  17  )
  18  
  19  var (
  20  	URL       string
  21  	GitRef    string
  22  	GitCommit string
  23  	BuildTime string
  24  	Tag       string
  25  )
  26  
  27  type command struct {
  28  	name string
  29  	args []string
  30  }
  31  
  32  var ldFlags []string
  33  
  34  func main() {
  35  	var e error
  36  	var ok bool
  37  	var home string
  38  	var list []string
  39  	if home, ok = os.LookupEnv("HOME"); !ok {
  40  		panic(e)
  41  	}
  42  	if len(os.Args) > 1 {
  43  		folderName := "test0"
  44  		var datadir string
  45  		if len(os.Args) > 2 {
  46  			datadir = os.Args[2]
  47  		} else {
  48  			datadir = filepath.Join(home, folderName)
  49  		}
  50  		if list, ok = commands[os.Args[1]]; ok {
  51  			for i := range list {
  52  				// inject the data directory
  53  				var split []string
  54  				out := strings.ReplaceAll(list[i], "%datadir", datadir)
  55  				split = strings.Split(out, " ")
  56  				fmt.Printf("executing item %d of list '%v' '%v' '%v'\n",
  57  					i, os.Args[1], split[0], split[1:],
  58  				)
  59  				var cmd *exec.Cmd
  60  				scriptPath := filepath.Join(appdata.Dir("stroy", false), "stroy.sh")
  61  				apputil.EnsureDir(scriptPath)
  62  				if e = ioutil.WriteFile(
  63  					scriptPath,
  64  					[]byte(strings.Join(split, " ")),
  65  					0700,
  66  				); e != nil {
  67  				} else {
  68  					cmd = exec.Command("sh", scriptPath)
  69  					cmd.Stdout = os.Stdout
  70  					cmd.Stdin = os.Stdin
  71  					cmd.Stderr = os.Stderr
  72  				}
  73  				if cmd == nil {
  74  					panic("cmd is nil")
  75  				}
  76  				var e error
  77  				if e = cmd.Start(); e != nil {
  78  					fmt.Fprintln(os.Stderr, e)
  79  					os.Exit(1)
  80  				}
  81  				if e := cmd.Wait(); e != nil {
  82  					os.Exit(1)
  83  				}
  84  			}
  85  		} else {
  86  			fmt.Println("command", os.Args[1], "not found")
  87  		}
  88  	} else {
  89  		fmt.Println("no command requested, available:")
  90  		for i := range commands {
  91  			fmt.Println(i)
  92  			for j := range commands[i] {
  93  				fmt.Println("\t" + commands[i][j])
  94  			}
  95  		}
  96  		fmt.Println()
  97  		fmt.Println(
  98  			"adding a second string to the commandline changes the path" +
  99  				" of the home folder selected in the scripts",
 100  		)
 101  	}
 102  }
 103