Esempio n. 1
0
func newACBuild() *lib.ACBuild {
	return lib.NewACBuild(contextpath, debug)
}
Esempio n. 2
0
// runWrapper return a func(cmd *cobra.Command, args []string) that internally
// will add command function return code and the reinsertion of the "--" flag
// terminator.
func runWrapper(cf func(cmd *cobra.Command, args []string) (exit int)) func(cmd *cobra.Command, args []string) {
	return func(cmd *cobra.Command, args []string) {
		if aciToModify == "" {
			cmdExitCode = cf(cmd, args)
			return
		}

		contextualCommands := []string{"begin", "write", "end", "version"}
		command := strings.Split(cmd.Use, " ")[0]
		for _, cc := range contextualCommands {
			if command == cc {
				stderr("Can't use the --modify flag with %s.", command)
				cmdExitCode = 1
				return
			}
		}

		var err error
		buildpath, err := ioutil.TempDir("", "acbuild-")
		if err != nil {
			stderr("%v", err)
			cmdExitCode = 1
			return
		}
		defer os.Remove(buildpath)

		finfo, err := os.Stat(aciToModify)
		switch {
		case os.IsNotExist(err):
			stderr("ACI doesn't appear to exist: %s.", aciToModify)
			cmdExitCode = 1
			return
		case err != nil:
			stderr("Error accessing ACI to modify: %v.", err)
			cmdExitCode = 1
			return
		case finfo.IsDir():
			stderr("ACI to modify is a directory: %s.", aciToModify)
			cmdExitCode = 1
			return
		}

		a := lib.NewACBuild(buildpath, debug)

		err = a.Begin(aciToModify, false)
		if err != nil {
			stderr("%v", err)
			cmdExitCode = 1
			return
		}

		cmdExitCode = cf(cmd, args)

		err = a.Write(aciToModify, true, false, nil)
		if err != nil {
			stderr("%v", err)
			cmdExitCode = 1
			return
		}

		err = a.End()
		if err != nil {
			stderr("%v", err)
			cmdExitCode = 1
			return
		}
	}
}