NewPkgManagersCommand function

Returns:

  • *cmdr.Command
Show/Hide Function Body
{
	// Root command
	cmd := cmdr.NewCommand(
		"pkgmanagers",
		apx.Trans("pkgmanagers.description"),
		apx.Trans("pkgmanagers.description"),
		nil,
	)

	// List subcommand
	listCmd := cmdr.NewCommand(
		"list",
		apx.Trans("pkgmanagers.list.description"),
		apx.Trans("pkgmanagers.list.description"),
		listPkgManagers,
	)

	listCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"json",
			"j",
			apx.Trans("pkgmanagers.list.options.json.description"),
			false,
		),
	)

	// Show subcommand
	showCmd := cmdr.NewCommand(
		"show",
		apx.Trans("pkgmanagers.show.description"),
		apx.Trans("pkgmanagers.show.description"),
		showPkgManager,
	)
	showCmd.Args = cobra.MinimumNArgs(1)

	// New subcommand
	newCmd := cmdr.NewCommand(
		"new",
		apx.Trans("pkgmanagers.new.description"),
		apx.Trans("pkgmanagers.new.description"),
		newPkgManager,
	)

	newCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"no-prompt",
			"y",
			apx.Trans("pkgmanagers.new.options.noPrompt.description"),
			false,
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("pkgmanagers.new.options.name.description"),
			"",
		),
	)
	newCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"need-sudo",
			"S",
			apx.Trans("pkgmanagers.new.options.needSudo.description"),
			false,
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"autoremove",
			"a",
			apx.Trans("pkgmanagers.new.options.autoremove.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"clean",
			"c",
			apx.Trans("pkgmanagers.new.options.clean.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"install",
			"i",
			apx.Trans("pkgmanagers.new.options.install.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"list",
			"l",
			apx.Trans("pkgmanagers.new.options.list.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"purge",
			"p",
			apx.Trans("pkgmanagers.new.options.purge.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"remove",
			"r",
			apx.Trans("pkgmanagers.new.options.remove.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"search",
			"s",
			apx.Trans("pkgmanagers.new.options.search.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"show",
			"w",
			apx.Trans("pkgmanagers.new.options.show.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"update",
			"u",
			apx.Trans("pkgmanagers.new.options.update.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"upgrade",
			"U",
			apx.Trans("pkgmanagers.new.options.upgrade.description"),
			"",
		),
	)

	// Rm subcommand
	rmCmd := cmdr.NewCommand(
		"rm",
		apx.Trans("pkgmanagers.rm.description"),
		apx.Trans("pkgmanagers.rm.description"),
		rmPkgManager,
	)

	rmCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("pkgmanagers.rm.options.name.description"),
			"",
		),
	)
	rmCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"force",
			"f",
			apx.Trans("pkgmanagers.rm.options.force.description"),
			false,
		),
	)

	// Export subcommand
	exportCmd := cmdr.NewCommand(
		"export",
		apx.Trans("pkgmanagers.export.description"),
		apx.Trans("pkgmanagers.export.description"),
		exportPkgmanager,
	)
	exportCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("pkgmanagers.export.options.name.description"),
			"",
		),
	)
	exportCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"output",
			"o",
			apx.Trans("stacks.export.options.output.description"),
			"",
		),
	)

	// Import subcommand
	importCmd := cmdr.NewCommand(
		"import",
		apx.Trans("pkgmanagers.import.description"),
		apx.Trans("pkgmanagers.import.description"),
		importPkgmanager,
	)
	importCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"input",
			"i",
			apx.Trans("pkgmanagers.import.options.input.description"),
			"",
		),
	)

	// Update subcommand
	updateCmd := cmdr.NewCommand(
		"update",
		apx.Trans("pkgmanagers.update.description"),
		apx.Trans("pkgmanagers.update.description"),
		updatePkgManager,
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("pkgmanagers.new.options.name.description"),
			"",
		),
	)
	updateCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"need-sudo",
			"S",
			apx.Trans("pkgmanagers.new.options.needSudo.description"),
			false,
		),
	)
	updateCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"no-prompt",
			"y",
			apx.Trans("pkgmanagers.new.options.noPrompt.description"),
			false,
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"autoremove",
			"a",
			apx.Trans("pkgmanagers.new.options.autoremove.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"clean",
			"c",
			apx.Trans("pkgmanagers.new.options.clean.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"install",
			"i",
			apx.Trans("pkgmanagers.new.options.install.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"list",
			"l",
			apx.Trans("pkgmanagers.new.options.list.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"purge",
			"p",
			apx.Trans("pkgmanagers.new.options.purge.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"remove",
			"r",
			apx.Trans("pkgmanagers.new.options.remove.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"search",
			"s",
			apx.Trans("pkgmanagers.new.options.search.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"show",
			"w",
			apx.Trans("pkgmanagers.new.options.show.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"update",
			"u",
			apx.Trans("pkgmanagers.new.options.update.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"upgrade",
			"U",
			apx.Trans("pkgmanagers.new.options.upgrade.description"),
			"",
		),
	)

	// Add subcommands to pkgmanagers
	cmd.AddCommand(listCmd)
	cmd.AddCommand(showCmd)
	cmd.AddCommand(newCmd)
	cmd.AddCommand(rmCmd)
	cmd.AddCommand(exportCmd)
	cmd.AddCommand(importCmd)
	cmd.AddCommand(updateCmd)

	return cmd
}

listPkgManagers function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	jsonFlag, _ := cmd.Flags().GetBool("json")

	pkgManagers := core.ListPkgManagers()

	if !jsonFlag {
		pkgManagersCount := len(pkgManagers)
		if pkgManagersCount == 0 {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.list.info.noPkgManagers"))
			return nil
		}

		cmdr.Info.Printfln(apx.Trans("pkgmanagers.list.info.foundPkgManagers"), pkgManagersCount)

		table := core.CreateApxTable(os.Stdout)
		table.SetHeader([]string{apx.Trans("pkgmanagers.labels.name"), apx.Trans("pkgmanagers.labels.builtIn")})

		for _, stack := range pkgManagers {
			builtIn := apx.Trans("apx.terminal.no")
			if stack.BuiltIn {
				builtIn = apx.Trans("apx.terminal.yes")
			}
			table.Append([]string{stack.Name, builtIn})
		}

		table.Render()
	} else {
		jsonPkgManagers, _ := json.MarshalIndent(pkgManagers, "", "  ")
		fmt.Println(string(jsonPkgManagers))
	}

	return nil
}

showPkgManager function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	pkgManagerName := args[0]
	pkgManager, err := core.LoadPkgManager(pkgManagerName)
	if err != nil {
		cmdr.Error.Println(err)
		return nil
	}

	table := core.CreateApxTable(os.Stdout)
	table.Append([]string{apx.Trans("pkgmanagers.labels.name"), pkgManager.Name})
	table.Append([]string{"NeedSudo", fmt.Sprintf("%t", pkgManager.NeedSudo)})
	table.Append([]string{"AutoRemove", pkgManager.CmdAutoRemove})
	table.Append([]string{"Clean", pkgManager.CmdClean})
	table.Append([]string{"Install", pkgManager.CmdInstall})
	table.Append([]string{"List", pkgManager.CmdList})
	table.Append([]string{"Purge", pkgManager.CmdPurge})
	table.Append([]string{"Remove", pkgManager.CmdRemove})
	table.Append([]string{"Search", pkgManager.CmdSearch})
	table.Append([]string{"Show", pkgManager.CmdShow})
	table.Append([]string{"Update", pkgManager.CmdUpdate})
	table.Append([]string{"Upgrade", pkgManager.CmdUpgrade})
	table.Render()

	return nil
}

newPkgManager function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	noPrompt, _ := cmd.Flags().GetBool("no-prompt")
	name, _ := cmd.Flags().GetString("name")
	needSudo, _ := cmd.Flags().GetBool("need-sudo")
	autoRemove, _ := cmd.Flags().GetString("autoremove")
	clean, _ := cmd.Flags().GetString("clean")
	install, _ := cmd.Flags().GetString("install")
	list, _ := cmd.Flags().GetString("list")
	purge, _ := cmd.Flags().GetString("purge")
	remove, _ := cmd.Flags().GetString("remove")
	search, _ := cmd.Flags().GetString("search")
	show, _ := cmd.Flags().GetString("show")
	update, _ := cmd.Flags().GetString("update")
	upgrade, _ := cmd.Flags().GetString("upgrade")

	reader := bufio.NewReader(os.Stdin)

	if name == "" {
		if noPrompt {
			cmdr.Error.Println(apx.Trans("pkgmanagers.new.error.noName"))
			return nil
		}

		cmdr.Info.Println(apx.Trans("pkgmanagers.new.info.askName"))
		name, _ = reader.ReadString('\n')
		name = strings.ReplaceAll(name, "\n", "")
		name = strings.ReplaceAll(name, " ", "")
		if name == "" {
			cmdr.Error.Println(apx.Trans("pkgmanagers.new.error.emptyName"))
			return nil
		}
	}

	if !needSudo && !noPrompt {
		validChoice := false
		for !validChoice {
			cmdr.Info.Println(apx.Trans("pkgmanagers.new.info.askSudo") + ` [y/N]`)
			answer, _ := reader.ReadString('\n')
			if answer == "\n" {
				answer = "n\n"
			}
			answer = strings.ToLower(strings.ReplaceAll(answer, " ", ""))
			switch answer {
			case "y\n":
				needSudo = true
				validChoice = true
			case "n\n":
				needSudo = false
				validChoice = true
			default:
				cmdr.Warning.Println(apx.Trans("apx.errors.invalidChoice"))
			}
		}
	}

	cmdMap := map[string]*string{
		PkgManagerCmdAutoRemove: &autoRemove,
		PkgManagerCmdClean:      &clean,
		PkgManagerCmdInstall:    &install,
		PkgManagerCmdList:       &list,
		PkgManagerCmdPurge:      &purge,
		PkgManagerCmdRemove:     &remove,
		PkgManagerCmdSearch:     &search,
		PkgManagerCmdShow:       &show,
		PkgManagerCmdUpdate:     &update,
		PkgManagerCmdUpgrade:    &upgrade,
	}

	for _, cmdName := range PkgManagerCmdSetOrder {
		cmd := cmdMap[cmdName]
		if *cmd == "" {
			if noPrompt {
				cmdr.Error.Printf(apx.Trans("pkgmanagers.new.error.noCommand"), cmdName)
				return nil
			}
			if cmdName == PkgManagerCmdPurge || cmdName == PkgManagerCmdAutoRemove {
				cmdr.Info.Printfln(apx.Trans("pkgmanagers.new.info.askCommandWithDefault"), cmdName, remove)
				*cmd, _ = reader.ReadString('\n')
				*cmd = strings.ReplaceAll(*cmd, "\n", "")
				if *cmd == "" {
					*cmd = remove
				}
				continue
			}

			cmdr.Info.Printfln(apx.Trans("pkgmanagers.new.info.askCommand"), cmdName)
			*cmd, _ = reader.ReadString('\n')
			*cmd = strings.ReplaceAll(*cmd, "\n", "")
			if *cmd == "" {
				cmdr.Error.Printf(apx.Trans("pkgmanagers.new.error.emptyCommand"), cmdName)
				return nil
			}
		}
	}

	if core.PkgManagerExists(name) {
		if noPrompt {
			cmdr.Error.Println(apx.Trans("pkgmanagers.new.error.alreadyExists"), name)
			return nil
		}

		cmdr.Info.Printfln(apx.Trans("pkgmanagers.new.info.askOverwrite"), name)
		answer, _ := reader.ReadString('\n')
		answer = strings.ReplaceAll(answer, "\n", "")

		if strings.ToLower(strings.TrimSpace(answer)) != "y" {
			cmdr.Info.Println(apx.Trans("apx.info.aborting"))
			return nil
		}
	}

	pkgManager := core.NewPkgManager(name, needSudo, autoRemove, clean, install, list, purge, remove, search, show, update, upgrade, false)
	err := pkgManager.Save()
	if err != nil {
		cmdr.Error.Println(err)
		return nil
	}

	cmdr.Success.Printfln(apx.Trans("pkgmanagers.new.success"), name)

	return nil
}

rmPkgManager function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	pkgManagerName, _ := cmd.Flags().GetString("name")
	if pkgManagerName == "" {
		cmdr.Error.Println(apx.Trans("pkgmanagers.rm.error.noName"))
		return nil
	}

	pkgManager, error := core.LoadPkgManager(pkgManagerName)
	if error != nil {
		return error
	}

	stacks := core.ListStackForPkgManager(pkgManager.Name)
	if len(stacks) > 0 {
		cmdr.Error.Printf(apx.Trans("pkgmanagers.rm.error.inUse"), len(stacks))
		table := core.CreateApxTable(os.Stdout)
		table.SetHeader([]string{apx.Trans("pkgmanagers.labels.name"), "Base", "Packages", "PkgManager", apx.Trans("pkgmanagers.labels.builtIn")})
		for _, stack := range stacks {
			builtIn := apx.Trans("apx.terminal.no")
			if stack.BuiltIn {
				builtIn = apx.Trans("apx.terminal.yes")
			}
			table.Append([]string{stack.Name, stack.Base, strings.Join(stack.Packages, ", "), stack.PkgManager, builtIn})
		}
		table.Render()
		return nil
	}

	force, _ := cmd.Flags().GetBool("force")

	if !force {
		reader := bufio.NewReader(os.Stdin)
		validChoice := false
		for !validChoice {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.rm.info.askConfirmation")+` [y/N]`, pkgManagerName)
			answer, _ := reader.ReadString('\n')
			if answer == "\n" {
				answer = "n\n"
			}
			answer = strings.ToLower(strings.ReplaceAll(answer, " ", ""))
			switch answer {
			case "y\n":
				validChoice = true
				force = true
			case "n\n":
				validChoice = true
			default:
				cmdr.Warning.Println(apx.Trans("apx.errors.invalidChoice"))
			}
		}
	}

	if !force {
		cmdr.Info.Printfln(apx.Trans("pkgmanagers.rm.info.aborting"), pkgManagerName)
		return nil
	}

	error = pkgManager.Remove()
	if error != nil {
		return error
	}

	cmdr.Info.Printfln(apx.Trans("pkgmanagers.rm.info.success"), pkgManagerName)
	return nil
}

exportPkgmanager function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	pkgManagerName, _ := cmd.Flags().GetString("name")
	if pkgManagerName == "" {
		cmdr.Error.Println(apx.Trans("pkgmanagers.export.error.noName"))
		return nil
	}

	pkgManager, error := core.LoadPkgManager(pkgManagerName)
	if error != nil {
		return error
	}

	output, _ := cmd.Flags().GetString("output")
	if output == "" {
		cmdr.Error.Println(apx.Trans("pkgmanagers.export.error.noOutput"))
		return nil
	}

	error = pkgManager.Export(output)
	if error != nil {
		return error
	}

	cmdr.Info.Printfln(apx.Trans("pkgmanagers.export.info.success"), pkgManager.Name, output)
	return nil
}

importPkgmanager function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	input, _ := cmd.Flags().GetString("input")
	if input == "" {
		cmdr.Error.Println(apx.Trans("pkgmanagers.import.error.noInput"))
		return nil
	}

	pkgmanager, error := core.LoadPkgManagerFromPath(input)
	if error != nil {
		cmdr.Error.Printf(apx.Trans("pkgmanagers.import.error.cannotLoad"), input)
	}

	error = pkgmanager.Save()
	if error != nil {
		return error
	}

	cmdr.Info.Printfln(apx.Trans("pkgmanagers.import.info.success"), pkgmanager.Name)
	return nil
}

updatePkgManager function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	name, _ := cmd.Flags().GetString("name")
	needSudo, _ := cmd.Flags().GetBool("need-sudo")
	noPrompt, _ := cmd.Flags().GetBool("no-prompt")
	autoRemove, _ := cmd.Flags().GetString("autoremove")
	clean, _ := cmd.Flags().GetString("clean")
	install, _ := cmd.Flags().GetString("install")
	list, _ := cmd.Flags().GetString("list")
	purge, _ := cmd.Flags().GetString("purge")
	remove, _ := cmd.Flags().GetString("remove")
	search, _ := cmd.Flags().GetString("search")
	show, _ := cmd.Flags().GetString("show")
	update, _ := cmd.Flags().GetString("update")
	upgrade, _ := cmd.Flags().GetString("upgrade")

	if name == "" {
		if len(args) != 1 || args[0] == "" {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.noName"))
			return nil
		}

		cmd.Flags().Set("name", args[0])
		name = args[0]
	}

	pkgmanager, error := core.LoadPkgManager(name)
	if error != nil {
		return error
	}

	if pkgmanager.BuiltIn {
		cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.builtIn"))
		os.Exit(126)
	}

	reader := bufio.NewReader(os.Stdin)

	if autoRemove == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "autoRemove", pkgmanager.CmdAutoRemove)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				autoRemove = pkgmanager.CmdAutoRemove
			} else {
				autoRemove = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "autoRemove")
			return nil
		}
	}

	if clean == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "clean", pkgmanager.CmdClean)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				clean = pkgmanager.CmdClean
			} else {
				clean = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "clean")
			return nil
		}
	}

	if install == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "install", pkgmanager.CmdInstall)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				install = pkgmanager.CmdInstall
			} else {
				install = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "install")
			return nil
		}
	}

	if list == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "list", pkgmanager.CmdList)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				list = pkgmanager.CmdList
			} else {
				list = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "list")
			return nil
		}
	}

	if purge == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "purge", pkgmanager.CmdPurge)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				purge = pkgmanager.CmdPurge
			} else {
				purge = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "purge")
			return nil
		}
	}

	if remove == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "remove", pkgmanager.CmdRemove)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				remove = pkgmanager.CmdRemove
			} else {
				remove = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "remove")
			return nil
		}
	}

	if search == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "search", pkgmanager.CmdSearch)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				search = pkgmanager.CmdSearch
			} else {
				search = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "search")
			return nil
		}
	}

	if show == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "show", pkgmanager.CmdShow)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				show = pkgmanager.CmdShow
			} else {
				show = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "show")
			return nil
		}
	}

	if update == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "update", pkgmanager.CmdUpdate)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				update = pkgmanager.CmdUpdate
			} else {
				update = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "update")
			return nil
		}
	}

	if upgrade == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.askNewCommand"), "upgrade", pkgmanager.CmdUpgrade)
			answer, _ := reader.ReadString('\n')
			answer = strings.TrimSpace(answer)
			if answer == "" {
				upgrade = pkgmanager.CmdUpgrade
			} else {
				upgrade = answer
			}
		} else {
			cmdr.Error.Println(apx.Trans("pkgmanagers.update.error.missingCommand"), "upgrade")
			return nil
		}
	}

	pkgmanager.NeedSudo = needSudo
	pkgmanager.CmdAutoRemove = autoRemove
	pkgmanager.CmdClean = clean
	pkgmanager.CmdInstall = install
	pkgmanager.CmdList = list
	pkgmanager.CmdPurge = purge
	pkgmanager.CmdRemove = remove
	pkgmanager.CmdSearch = search
	pkgmanager.CmdShow = show
	pkgmanager.CmdUpdate = update
	pkgmanager.CmdUpgrade = upgrade

	err := pkgmanager.Save()
	if err != nil {
		return err
	}

	cmdr.Info.Printfln(apx.Trans("pkgmanagers.update.info.success"), name)

	return nil
}

New function

Parameters:

  • version string
  • fs embed.FS

Returns:

  • *cmdr.App
Show/Hide Function Body
{
	apx = cmdr.NewApp("apx", version, fs)
	return apx
}

NewRootCommand function

Parameters:

  • version string

Returns:

  • *cmdr.Command
Show/Hide Function Body
{
	root := cmdr.NewCommand(
		"apx",
		apx.Trans("apx.description"),
		apx.Trans("apx.description"),
		nil,
	)
	root.Version = version

	return root
}

NewRuntimeCommands function

Returns:

  • []*cmdr.Command
Show/Hide Function Body
{
	var commands []*cmdr.Command

	subSystems, err := core.ListSubSystems(false, false)
	if err != nil {
		return []*cmdr.Command{}
	}

	handleFunc := func(subSystem *core.SubSystem, reqFunc func(*core.SubSystem, string, *cobra.Command, []string) error) func(cmd *cobra.Command, args []string) error {
		return func(cmd *cobra.Command, args []string) error {
			return reqFunc(subSystem, cmd.Name(), cmd, args)
		}
	}

	for _, subSystem := range subSystems {
		subSystemCmd := cmdr.NewCommand(
			subSystem.Name,
			apx.Trans("runtimeCommand.description"),
			apx.Trans("runtimeCommand.description"),
			nil,
		)

		autoRemoveCmd := cmdr.NewCommand(
			"autoremove",
			apx.Trans("runtimeCommand.autoremove.description"),
			apx.Trans("runtimeCommand.autoremove.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		cleanCmd := cmdr.NewCommand(
			"clean",
			apx.Trans("runtimeCommand.clean.description"),
			apx.Trans("runtimeCommand.clean.description"),
			handleFunc(subSystem, runPkgCmd),
		)

		installCmd := cmdr.NewCommand(
			"install",
			apx.Trans("runtimeCommand.install.description"),
			apx.Trans("runtimeCommand.install.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		installCmd.WithBoolFlag(
			cmdr.NewBoolFlag(
				"no-export",
				"n",
				apx.Trans("runtimeCommand.install.options.noExport.description"),
				false,
			),
		)

		listCmd := cmdr.NewCommand(
			"list",
			apx.Trans("runtimeCommand.list.description"),
			apx.Trans("runtimeCommand.list.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		purgeCmd := cmdr.NewCommand(
			"purge",
			apx.Trans("runtimeCommand.purge.description"),
			apx.Trans("runtimeCommand.purge.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		removeCmd := cmdr.NewCommand(
			"remove",
			apx.Trans("runtimeCommand.remove.description"),
			apx.Trans("runtimeCommand.remove.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		searchCmd := cmdr.NewCommand(
			"search",
			apx.Trans("runtimeCommand.search.description"),
			apx.Trans("runtimeCommand.search.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		showCmd := cmdr.NewCommand(
			"show",
			apx.Trans("runtimeCommand.show.description"),
			apx.Trans("runtimeCommand.show.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		updateCmd := cmdr.NewCommand(
			"update",
			apx.Trans("runtimeCommand.update.description"),
			apx.Trans("runtimeCommand.update.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		upgradeCmd := cmdr.NewCommand(
			"upgrade",
			apx.Trans("runtimeCommand.upgrade.description"),
			apx.Trans("runtimeCommand.upgrade.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		runCmd := cmdr.NewCommand(
			"run",
			apx.Trans("runtimeCommand.run.description"),
			apx.Trans("runtimeCommand.run.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		enterCmd := cmdr.NewCommand(
			"enter",
			apx.Trans("runtimeCommand.enter.description"),
			apx.Trans("runtimeCommand.enter.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		exportCmd := cmdr.NewCommand(
			"export",
			apx.Trans("runtimeCommand.export.description"),
			apx.Trans("runtimeCommand.export.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		exportCmd.WithStringFlag(
			cmdr.NewStringFlag(
				"app-name",
				"a",
				apx.Trans("runtimeCommand.export.options.appName.description"),
				"",
			),
		)
		exportCmd.WithStringFlag(
			cmdr.NewStringFlag(
				"bin",
				"b",
				apx.Trans("runtimeCommand.export.options.bin.description"),
				"",
			),
		)
		exportCmd.WithStringFlag(
			cmdr.NewStringFlag(
				"bin-output",
				"o",
				apx.Trans("runtimeCommand.export.options.binOutput.description"),
				"",
			),
		)
		unexportCmd := cmdr.NewCommand(
			"unexport",
			apx.Trans("runtimeCommand.unexport.description"),
			apx.Trans("runtimeCommand.unexport.description"),
			handleFunc(subSystem, runPkgCmd),
		)
		unexportCmd.WithStringFlag(
			cmdr.NewStringFlag(
				"app-name",
				"a",
				apx.Trans("runtimeCommand.unexport.options.appName.description"),
				"",
			),
		)
		unexportCmd.WithStringFlag(
			cmdr.NewStringFlag(
				"bin",
				"b",
				apx.Trans("runtimeCommand.unexport.options.bin.description"),
				"",
			),
		)
		unexportCmd.WithStringFlag(
			cmdr.NewStringFlag(
				"bin-output",
				"o",
				apx.Trans("runtimeCommand.unexport.options.binOutput.description"),
				"",
			),
		)

		startCmd := cmdr.NewCommand(
			"start",
			apx.Trans("runtimeCommand.start.description"),
			apx.Trans("runtimeCommand.start.description"),
			handleFunc(subSystem, runPkgCmd),
		)

		stopCmd := cmdr.NewCommand(
			"stop",
			apx.Trans("runtimeCommand.stop.description"),
			apx.Trans("runtimeCommand.stop.description"),
			handleFunc(subSystem, runPkgCmd),
		)

		subSystemCmd.AddCommand(autoRemoveCmd)
		subSystemCmd.AddCommand(cleanCmd)
		subSystemCmd.AddCommand(installCmd)
		subSystemCmd.AddCommand(listCmd)
		subSystemCmd.AddCommand(purgeCmd)
		subSystemCmd.AddCommand(removeCmd)
		subSystemCmd.AddCommand(searchCmd)
		subSystemCmd.AddCommand(showCmd)
		subSystemCmd.AddCommand(updateCmd)
		subSystemCmd.AddCommand(upgradeCmd)
		subSystemCmd.AddCommand(runCmd)
		subSystemCmd.AddCommand(enterCmd)
		subSystemCmd.AddCommand(exportCmd)
		subSystemCmd.AddCommand(unexportCmd)
		subSystemCmd.AddCommand(startCmd)
		subSystemCmd.AddCommand(stopCmd)

		commands = append(commands, subSystemCmd)
	}

	return commands
}

isBaseCommand function

isBaseCommand informs whether the command is a subsystem-base command

(e.g. run, enter) instead of a subsystem-specific one (e.g. install, update)

Parameters:

  • command string

Returns:

  • bool
Show/Hide Function Body
{
	return slices.Contains(baseCmds, command)
}

pkgManagerCommands function

pkgManagerCommands maps command line arguments into package manager commands

Parameters:

  • pkgManager *core.PkgManager
  • command string

Returns:

  • string
  • error
Show/Hide Function Body
{
	switch command {
	case "autoremove":
		return pkgManager.CmdAutoRemove, nil
	case "clean":
		return pkgManager.CmdClean, nil
	case "install":
		return pkgManager.CmdInstall, nil
	case "list":
		return pkgManager.CmdList, nil
	case "purge":
		return pkgManager.CmdPurge, nil
	case "remove":
		return pkgManager.CmdRemove, nil
	case "search":
		return pkgManager.CmdSearch, nil
	case "show":
		return pkgManager.CmdShow, nil
	case "update":
		return pkgManager.CmdUpdate, nil
	case "upgrade":
		return pkgManager.CmdUpgrade, nil
	default:
		return "", fmt.Errorf(apx.Trans("apx.errors.unknownCommand"), command)
	}
}

runPkgCmd function

Parameters:

  • subSystem *core.SubSystem
  • command string
  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	if !isBaseCommand(command) {
		pkgManager, err := subSystem.Stack.GetPkgManager()
		if err != nil {
			return fmt.Errorf(apx.Trans("runtimeCommand.error.cantAccessPkgManager"), err)
		}

		realCommand, err := pkgManagerCommands(pkgManager, command)
		if err != nil {
			return err
		}

		if command == "remove" {
			exportedN, err := subSystem.UnexportDesktopEntries(args...)
			if err == nil {
				cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.unexportedApps"), exportedN)
			}
		}

		finalArgs := pkgManager.GenCmd(realCommand, args...)
		_, err = subSystem.Exec(false, false, finalArgs...)
		if err != nil {
			return fmt.Errorf(apx.Trans("runtimeCommand.error.executingCommand"), err)
		}

		if command == "install" && !cmd.Flag("no-export").Changed {
			exportedN, err := subSystem.ExportDesktopEntries(args...)
			if err == nil {
				cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.exportedApps"), exportedN)
			}
		}

		return nil
	}

	if command == "run" {
		_, err := subSystem.Exec(false, false, args...)
		if err != nil {
			return fmt.Errorf(apx.Trans("runtimeCommand.error.executingCommand"), err)
		}

		return nil
	}

	if command == "enter" {
		err := subSystem.Enter()
		if err != nil {
			return fmt.Errorf(apx.Trans("runtimeCommand.error.enteringContainer"), err)
		}

		return nil
	}

	if command == "export" || command == "unexport" {
		appName, _ := cmd.Flags().GetString("app-name")
		bin, _ := cmd.Flags().GetString("bin")
		binOutput, _ := cmd.Flags().GetString("bin-output")

		return handleExport(subSystem, command, appName, bin, binOutput)
	}

	if command == "start" {
		cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.startingContainer"), subSystem.Name)
		err := subSystem.Start()
		if err != nil {
			return fmt.Errorf(apx.Trans("runtimeCommand.error.startingContainer"), err)
		}

		cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.startedContainer"))
	}

	if command == "stop" {
		cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.stoppingContainer"), subSystem.Name)
		err := subSystem.Stop()
		if err != nil {
			return fmt.Errorf(apx.Trans("runtimeCommand.error.stoppingContainer"), err)
		}

		cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.stoppedContainer"))
	}

	return nil
}

handleExport function

Parameters:

  • subSystem *core.SubSystem
  • command string
  • appName string
  • bin string
  • binOutput string

Returns:

  • error
Show/Hide Function Body
{
	if appName == "" && bin == "" {
		return fmt.Errorf(apx.Trans("runtimeCommand.error.noAppNameOrBin"))
	}

	if appName != "" && bin != "" {
		return fmt.Errorf(apx.Trans("runtimeCommand.error.sameAppOrBin"))
	}

	if command == "export" {
		if appName != "" {
			err := subSystem.ExportDesktopEntry(appName)
			if err != nil {
				return fmt.Errorf(apx.Trans("runtimeCommand.error.exportingApp"), err)
			}

			cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.exportedApp"), appName)
		} else {
			err := subSystem.ExportBin(bin, binOutput)
			if err != nil {
				return fmt.Errorf(apx.Trans("runtimeCommand.error.exportingBin"), err)
			}

			cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.exportedBin"), bin)
		}
	} else {
		if appName != "" {
			err := subSystem.UnexportDesktopEntry(appName)
			if err != nil {
				return fmt.Errorf(apx.Trans("runtimeCommand.error.unexportingApp"), err)
			}

			cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.unexportedApp"), appName)
		} else {
			err := subSystem.UnexportBin(bin, binOutput)
			if err != nil {
				return fmt.Errorf(apx.Trans("runtimeCommand.error.unexportingBin"), err)
			}

			cmdr.Info.Printfln(apx.Trans("runtimeCommand.info.unexportedBin"), bin)
		}
	}

	return nil
}

NewStacksCommand function

Returns:

  • *cmdr.Command
Show/Hide Function Body
{
	// Root command
	cmd := cmdr.NewCommand(
		"stacks",
		apx.Trans("stacks.description"),
		apx.Trans("stacks.description"),
		nil,
	)

	// List subcommand
	listCmd := cmdr.NewCommand(
		"list",
		apx.Trans("stacks.list.description"),
		apx.Trans("stacks.list.description"),
		listStacks,
	)

	listCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"json",
			"j",
			apx.Trans("stacks.list.options.json.description"),
			false,
		),
	)

	// Show subcommand
	showCmd := cmdr.NewCommand(
		"show",
		apx.Trans("stacks.show.description"),
		apx.Trans("stacks.show.description"),
		showStack,
	)
	showCmd.Args = cobra.MinimumNArgs(1)

	// New subcommand
	newCmd := cmdr.NewCommand(
		"new",
		apx.Trans("stacks.new.description"),
		apx.Trans("stacks.new.description"),
		newStack,
	)
	newCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"no-prompt",
			"y",
			apx.Trans("stacks.new.options.noPrompt.description"),
			false,
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("stacks.new.options.name.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"base",
			"b",
			apx.Trans("stacks.new.options.base.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"packages",
			"p",
			apx.Trans("stacks.new.options.packages.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"pkg-manager",
			"k",
			apx.Trans("stacks.new.options.pkgManager.description"),
			"",
		),
	)

	// Update subcommand
	updateCmd := cmdr.NewCommand(
		"update",
		apx.Trans("stacks.update.description"),
		apx.Trans("stacks.update.description"),
		updateStack,
	)
	updateCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"no-prompt",
			"y",
			apx.Trans("stacks.update.options.noPrompt.description"),
			false,
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("stacks.update.options.name.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"base",
			"b",
			apx.Trans("stacks.update.options.base.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"packages",
			"p",
			apx.Trans("stacks.update.options.packages.description"),
			"",
		),
	)
	updateCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"pkg-manager",
			"k",
			apx.Trans("stacks.update.options.pkgManager.description"),
			"",
		),
	)

	// Rm subcommand
	rmStackCmd := cmdr.NewCommand(
		"rm",
		apx.Trans("stacks.rm.description"),
		apx.Trans("stacks.rm.description"),
		removeStack,
	)

	rmStackCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("stacks.rm.options.name.description"),
			"",
		),
	)
	rmStackCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"force",
			"f",
			apx.Trans("stacks.rm.options.force.description"),
			false,
		),
	)

	// Export subcommand
	exportCmd := cmdr.NewCommand(
		"export",
		apx.Trans("stacks.export.description"),
		apx.Trans("stacks.export.description"),
		exportStack,
	)
	exportCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("stacks.export.options.name.description"),
			"",
		),
	)
	exportCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"output",
			"o",
			apx.Trans("stacks.export.options.output.description"),
			"",
		),
	)

	// Import subcommand
	importCmd := cmdr.NewCommand(
		"import",
		apx.Trans("stacks.import.description"),
		apx.Trans("stacks.import.description"),
		importStack,
	)
	importCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"input",
			"i",
			apx.Trans("stacks.import.options.input.description"),
			"",
		),
	)

	// Add subcommands to stacks
	cmd.AddCommand(listCmd)
	cmd.AddCommand(showCmd)
	cmd.AddCommand(newCmd)
	cmd.AddCommand(updateCmd)
	cmd.AddCommand(rmStackCmd)
	cmd.AddCommand(exportCmd)
	cmd.AddCommand(importCmd)

	return cmd
}

listStacks function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	jsonFlag, _ := cmd.Flags().GetBool("json")

	stacks := core.ListStacks()

	if !jsonFlag {
		stacksCount := len(stacks)
		if stacksCount == 0 {
			fmt.Println(apx.Trans("stacks.list.info.noStacks"))
			return nil
		}

		cmdr.Info.Printfln(apx.Trans("stacks.list.info.foundStacks"), stacksCount)

		table := core.CreateApxTable(os.Stdout)
		table.SetHeader([]string{apx.Trans("stacks.labels.name"), "Base", apx.Trans("stacks.labels.builtIn"), "Pkgs", "Pkg manager"})

		for _, stack := range stacks {
			builtIn := apx.Trans("apx.terminal.no")
			if stack.BuiltIn {
				builtIn = apx.Trans("apx.terminal.yes")
			}
			table.Append([]string{stack.Name, stack.Base, builtIn, fmt.Sprintf("%d", len(stack.Packages)), stack.PkgManager})
		}

		table.Render()
	} else {
		jsonStacks, err := json.MarshalIndent(stacks, "", "  ")
		if err != nil {
			return err
		}

		fmt.Println(string(jsonStacks))
	}

	return nil
}

showStack function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	stack, error := core.LoadStack(args[0])
	if error != nil {
		return error
	}

	table := core.CreateApxTable(os.Stdout)
	table.Append([]string{apx.Trans("stacks.labels.name"), stack.Name})
	table.Append([]string{"Base", stack.Base})
	table.Append([]string{"Packages", strings.Join(stack.Packages, ", ")})
	table.Append([]string{"Package manager", stack.PkgManager})
	table.Render()

	return nil
}

newStack function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	noPrompt, _ := cmd.Flags().GetBool("no-prompt")
	name, _ := cmd.Flags().GetString("name")
	base, _ := cmd.Flags().GetString("base")
	packages, _ := cmd.Flags().GetString("packages")
	pkgManager, _ := cmd.Flags().GetString("pkg-manager")

	if name == "" {
		if !noPrompt {
			cmdr.Info.Println(apx.Trans("stacks.new.info.askName"))
			fmt.Scanln(&name)
			if name == "" {
				cmdr.Error.Println(apx.Trans("stacks.new.error.emptyName"))
				return nil
			}
		} else {
			cmdr.Error.Println(apx.Trans("stacks.new.error.noName"))
			return nil
		}
	}

	ok := core.StackExists(name)
	if ok {
		cmdr.Error.Printfln(apx.Trans("stacks.new.error.alreadyExists"), name)
		return nil
	}

	if base == "" {
		if !noPrompt {
			cmdr.Info.Println(apx.Trans("stacks.new.info.askBase"))
			fmt.Scanln(&base)
			if base == "" {
				cmdr.Error.Println(apx.Trans("stacks.new.error.emptyBase"))
				return nil
			}
		} else {
			cmdr.Error.Println(apx.Trans("stacks.new.error.noBase"))
			return nil
		}
	}

	if pkgManager == "" {
		pkgManagers := core.ListPkgManagers()
		if len(pkgManagers) == 0 {
			cmdr.Error.Println(apx.Trans("stacks.new.error.noPkgManagers"))
			return nil
		}

		cmdr.Info.Println(apx.Trans("stacks.new.info.askPkgManager"))
		for i, manager := range pkgManagers {
			fmt.Printf("%d. %s\n", i+1, manager.Name)
		}
		cmdr.Info.Printfln(apx.Trans("stacks.new.info.selectPkgManager"), len(pkgManagers))
		var pkgManagerIndex int
		_, err := fmt.Scanln(&pkgManagerIndex)
		if err != nil {
			cmdr.Error.Println(apx.Trans("apx.errors.invalidInput"))
			return nil
		}

		if pkgManagerIndex < 1 || pkgManagerIndex > len(pkgManagers) {
			cmdr.Error.Println(apx.Trans("apx.errors.invalidInput"))
			return nil
		}

		pkgManager = pkgManagers[pkgManagerIndex-1].Name
	}

	ok = core.PkgManagerExists(pkgManager)
	if !ok {
		cmdr.Error.Println(apx.Trans("stacks.new.error.pkgManagerDoesNotExist"))
		return nil
	}

	packagesArray := strings.Fields(packages)
	if len(packagesArray) == 0 && !noPrompt {
		cmdr.Info.Println(apx.Trans("stacks.new.info.noPackages") + "[y/N]")
		reader := bufio.NewReader(os.Stdin)
		answer, _ := reader.ReadString('\n')
		answer = strings.TrimSpace(answer)
		if answer == "y" || answer == "Y" {
			cmdr.Info.Println(apx.Trans("stacks.new.info.askPackages"))
			packagesInput, _ := reader.ReadString('\n')
			packagesInput = strings.TrimSpace(packagesInput)
			packagesArray = strings.Fields(packagesInput)
		} else {
			packagesArray = []string{}
		}
	}

	stack := core.NewStack(name, base, packagesArray, pkgManager, false)

	err := stack.Save()
	if err != nil {
		return err
	}

	cmdr.Success.Printfln(apx.Trans("stacks.new.info.success"), name)

	return nil
}

updateStack function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	noPrompt, _ := cmd.Flags().GetBool("no-prompt")
	name, _ := cmd.Flags().GetString("name")
	base, _ := cmd.Flags().GetString("base")
	packages, _ := cmd.Flags().GetString("packages")
	pkgManager, _ := cmd.Flags().GetString("pkg-manager")

	if name == "" {
		if len(args) != 1 || args[0] == "" {
			cmdr.Error.Println(apx.Trans("stacks.update.error.noName"))
			return nil
		}

		cmd.Flags().Set("name", args[0])
		name = args[0]
	}

	stack, error := core.LoadStack(name)
	if error != nil {
		return error
	}

	if stack.BuiltIn {
		cmdr.Error.Println(apx.Trans("stacks.update.error.builtIn"))
		os.Exit(126)
	}

	if base == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("stacks.update.info.askBase"), stack.Base)
			fmt.Scanln(&base)
			if base == "" {
				base = stack.Base
			}
		} else {
			cmdr.Error.Println(apx.Trans("stacks.update.error.noBase"))
			return nil
		}
	}

	if pkgManager == "" {
		if !noPrompt {
			cmdr.Info.Printfln(apx.Trans("stacks.update.info.askPkgManager"), stack.PkgManager)
			fmt.Scanln(&pkgManager)
			if pkgManager == "" {
				pkgManager = stack.PkgManager
			}
		} else {
			cmdr.Error.Println(apx.Trans("stacks.update.error.noPkgManager"))
			return nil
		}
	}

	ok := core.PkgManagerExists(pkgManager)
	if !ok {
		cmdr.Error.Println(apx.Trans("stacks.update.error.pkgManagerDoesNotExist"))
		return nil
	}


	if len(packages) > 0 {
		stack.Packages = strings.Fields(packages)
	} else if !noPrompt {
		if len(stack.Packages) > 0 {
			cmdr.Info.Println(apx.Trans("stacks.update.info.confirmPackages") + "[y/N]"  + "\n\t -", strings.Join(stack.Packages, "\n\t - "))
		} else {
			cmdr.Info.Println(apx.Trans("stacks.update.info.noPackages") + "[y/N]")
		}
		reader := bufio.NewReader(os.Stdin)
		answer, _ := reader.ReadString('\n')
		answer = strings.TrimSpace(answer)

		packagesArray := []string{}

		if answer == "y" || answer == "Y" {
			cmdr.Info.Println(apx.Trans("stacks.update.info.askPackages"))
			packagesInput, _ := reader.ReadString('\n')
			packagesInput = strings.TrimSpace(packagesInput)
			packagesArray = strings.Fields(packagesInput)
			stack.Packages = packagesArray
		}
	}

	stack.Base = base
	stack.PkgManager = pkgManager

	err := stack.Save()
	if err != nil {
		return err
	}

	cmdr.Info.Printfln(apx.Trans("stacks.update.info.success"), name)

	return nil
}

removeStack function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	stackName, _ := cmd.Flags().GetString("name")
	if stackName == "" {
		cmdr.Error.Println(apx.Trans("stacks.rm.error.noName"))
		return nil
	}

	subSystems, _ := core.ListSubsystemForStack(stackName)
	if len(subSystems) > 0 {
		cmdr.Error.Printfln(apx.Trans("stacks.rm.error.inUse"), len(subSystems))
		table := core.CreateApxTable(os.Stdout)
		table.SetHeader([]string{apx.Trans("subsystems.labels.name"), "Stack", apx.Trans("subsystems.labels.status"), "Pkgs"})
		for _, subSystem := range subSystems {
			table.Append([]string{
				subSystem.Name,
				subSystem.Stack.Name,
				subSystem.Status,
				fmt.Sprintf("%d", len(subSystem.Stack.Packages)),
			})
		}
		table.Render()
		return nil
	}

	force, _ := cmd.Flags().GetBool("force")

	if !force {
		reader := bufio.NewReader(os.Stdin)
		validChoice := false
		for !validChoice {
			cmdr.Info.Printfln(apx.Trans("stacks.rm.info.askConfirmation")+` [y/N]`, stackName)
			answer, _ := reader.ReadString('\n')
			if answer == "\n" {
				answer = "n\n"
			}
			answer = strings.ToLower(strings.ReplaceAll(answer, " ", ""))
			switch answer {
			case "y\n":
				validChoice = true
				force = true
			case "n\n":
				validChoice = true
			default:
				cmdr.Warning.Println(apx.Trans("apx.errors.invalidChoice"))
			}
		}
	}

	if !force {
		cmdr.Info.Printfln(apx.Trans("pkgmanagers.rm.info.aborting"), stackName)
		return nil
	}

	stack, error := core.LoadStack(stackName)
	if error != nil {
		return error
	}

	error = stack.Remove()
	if error != nil {
		return error
	}

	cmdr.Info.Printfln(apx.Trans("stacks.rm.info.success"), stackName)
	return nil
}

exportStack function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	stackName, _ := cmd.Flags().GetString("name")
	if stackName == "" {
		cmdr.Error.Println(apx.Trans("stacks.export.error.noName"))
		return nil
	}

	stack, error := core.LoadStack(stackName)
	if error != nil {
		return error
	}

	output, _ := cmd.Flags().GetString("output")
	if output == "" {
		cmdr.Error.Println(apx.Trans("stacks.export.error.noOutput"))
		return nil
	}

	error = stack.Export(output)
	if error != nil {
		return error
	}

	cmdr.Info.Printfln(apx.Trans("stacks.export.info.success"), stack.Name, output)
	return nil
}

importStack function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	input, _ := cmd.Flags().GetString("input")
	if input == "" {
		cmdr.Error.Println(apx.Trans("stacks.import.error.noInput"))
		return nil
	}

	stack, error := core.LoadStackFromPath(input)
	if error != nil {
		cmdr.Error.Printf(apx.Trans("stacks.import.error.cannotLoad"), input)
	}

	error = stack.Save()
	if error != nil {
		return error
	}

	cmdr.Info.Printfln(apx.Trans("stacks.import.info.success"), stack.Name)
	return nil
}

NewSubSystemsCommand function

Returns:

  • *cmdr.Command
Show/Hide Function Body
{
	// Root command
	cmd := cmdr.NewCommand(
		"subsystems",
		apx.Trans("subsystems.description"),
		apx.Trans("subsystems.description"),
		nil,
	)

	// List subcommand
	listCmd := cmdr.NewCommand(
		"list",
		apx.Trans("subsystems.list.description"),
		apx.Trans("subsystems.list.description"),
		listSubSystems,
	)

	listCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"json",
			"j",
			apx.Trans("subsystems.list.options.json.description"),
			false,
		),
	)

	// New subcommand
	newCmd := cmdr.NewCommand(
		"new",
		apx.Trans("subsystems.new.description"),
		apx.Trans("subsystems.new.description"),
		newSubSystem,
	)

	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"stack",
			"s",
			apx.Trans("subsystems.new.options.stack.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("subsystems.new.options.name.description"),
			"",
		),
	)
	newCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"home",
			"H",
			apx.Trans("subsystems.new.options.home.description"),
			"",
		),
	)
	newCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"init",
			"i",
			apx.Trans("subsystems.new.options.init.description"),
			false,
		),
	)

	// Rm subcommand
	rmCmd := cmdr.NewCommand(
		"rm",
		apx.Trans("subsystems.rm.description"),
		apx.Trans("subsystems.rm.description"),
		rmSubSystem,
	)

	rmCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("subsystems.rm.options.name.description"),
			"",
		),
	)
	rmCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"force",
			"f",
			apx.Trans("subsystems.rm.options.force.description"),
			false,
		),
	)

	// Reset subcommand
	resetCmd := cmdr.NewCommand(
		"reset",
		apx.Trans("subsystems.reset.description"),
		apx.Trans("subsystems.reset.description"),
		resetSubSystem,
	)

	resetCmd.WithStringFlag(
		cmdr.NewStringFlag(
			"name",
			"n",
			apx.Trans("subsystems.reset.options.name.description"),
			"",
		),
	)
	resetCmd.WithBoolFlag(
		cmdr.NewBoolFlag(
			"force",
			"f",
			apx.Trans("subsystems.reset.options.force.description"),
			false,
		),
	)

	// Add subcommands to subsystems
	cmd.AddCommand(listCmd)
	cmd.AddCommand(newCmd)
	cmd.AddCommand(rmCmd)
	cmd.AddCommand(resetCmd)

	return cmd
}

listSubSystems function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	jsonFlag, _ := cmd.Flags().GetBool("json")

	subSystems, err := core.ListSubSystems(false, false)
	if err != nil {
		return err
	}

	if !jsonFlag {
		subSystemsCount := len(subSystems)
		if subSystemsCount == 0 {
			cmdr.Info.Println(apx.Trans("subsystems.list.info.noSubsystems"))
			return nil
		}

		cmdr.Info.Printfln(apx.Trans("subsystems.list.info.foundSubsystems"), subSystemsCount)

		table := core.CreateApxTable(os.Stdout)
		table.SetHeader([]string{apx.Trans("subsystems.labels.name"), "Stack", apx.Trans("subsystems.labels.status"), "Pkgs"})

		for _, subSystem := range subSystems {
			table.Append([]string{
				subSystem.Name,
				subSystem.Stack.Name,
				subSystem.Status,
				fmt.Sprintf("%d", len(subSystem.Stack.Packages)),
			})
		}

		table.Render()
	} else {
		jsonSubSystems, err := json.MarshalIndent(subSystems, "", "  ")
		if err != nil {
			return err
		}

		fmt.Println(string(jsonSubSystems))
	}

	return nil
}

newSubSystem function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	home, _ := cmd.Flags().GetString("home")
	stackName, _ := cmd.Flags().GetString("stack")
	subSystemName, _ := cmd.Flags().GetString("name")
	isInit, _ := cmd.Flags().GetBool("init")

	stacks := core.ListStacks()
	if len(stacks) == 0 {
		cmdr.Error.Println(apx.Trans("subsystems.new.error.noStacks"))
		return nil
	}

	if subSystemName == "" {
		cmdr.Info.Println(apx.Trans("subsystems.new.info.askName"))
		fmt.Scanln(&subSystemName)
		if subSystemName == "" {
			cmdr.Error.Println(apx.Trans("subsystems.new.error.emptyName"))
			return nil
		}
	}

	if stackName == "" {
		cmdr.Info.Println(apx.Trans("subsystems.new.info.availableStacks"))
		for i, stack := range stacks {
			fmt.Printf("%d. %s\n", i+1, stack.Name)
		}
		cmdr.Info.Printfln(apx.Trans("subsystems.new.info.selectStack"), len(stacks))

		var stackIndex int
		_, err := fmt.Scanln(&stackIndex)
		if err != nil {
			cmdr.Error.Println(apx.Trans("apx.errors.invalidInput"))
			return nil
		}

		if stackIndex < 1 || stackIndex > len(stacks) {
			cmdr.Error.Println(apx.Trans("apx.errors.invalidInput"))
			return nil
		}

		stackName = stacks[stackIndex-1].Name
	}

	checkSubSystem, err := core.LoadSubSystem(subSystemName, false)
	if err == nil {
		cmdr.Error.Printf(apx.Trans("subsystems.new.error.alreadyExists"), checkSubSystem.Name)
		return nil
	}

	stack, err := core.LoadStack(stackName)
	if err != nil {
		return err
	}

	subSystem, err := core.NewSubSystem(subSystemName, stack, home, isInit, false, false, false, true, "")
	if err != nil {
		return err
	}

	spinner, _ := cmdr.Spinner.Start(fmt.Sprintf(apx.Trans("subsystems.new.info.creatingSubsystem"), subSystemName, stackName))
	err = subSystem.Create()
	if err != nil {
		return err
	}

	spinner.UpdateText(fmt.Sprintf(apx.Trans("subsystems.new.info.success"), subSystemName))
	spinner.Success()

	return nil
}

rmSubSystem function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	subSystemName, _ := cmd.Flags().GetString("name")
	forceFlag, _ := cmd.Flags().GetBool("force")

	if subSystemName == "" {
		cmdr.Error.Println(apx.Trans("subsystems.rm.error.noName"))
		return nil
	}

	if !forceFlag {
		cmdr.Info.Printfln(apx.Trans("subsystems.rm.info.askConfirmation")+` [y/N]`, subSystemName)
		var confirmation string
		fmt.Scanln(&confirmation)
		if strings.ToLower(confirmation) != "y" {
			cmdr.Info.Println(apx.Trans("apx.info.aborting"))
			return nil
		}
	}

	subSystem, err := core.LoadSubSystem(subSystemName, false)
	if err != nil {
		return err
	}

	err = subSystem.Remove()
	if err != nil {
		return err
	}

	cmdr.Success.Printfln(apx.Trans("subsystems.rm.info.success"), subSystemName)

	return nil
}

resetSubSystem function

Parameters:

  • cmd *cobra.Command
  • args []string

Returns:

  • error
Show/Hide Function Body
{
	subSystemName, _ := cmd.Flags().GetString("name")
	if subSystemName == "" {
		cmdr.Error.Println(apx.Trans("subsystems.reset.error.noName"))
		return nil
	}

	forceFlag, _ := cmd.Flags().GetBool("force")

	if !forceFlag {
		cmdr.Info.Printfln(apx.Trans("subsystems.reset.info.askConfirmation")+` [y/N]`, subSystemName)
		var confirmation string
		fmt.Scanln(&confirmation)
		if strings.ToLower(confirmation) != "y" {
			cmdr.Info.Println(apx.Trans("apx.info.aborting"))
			return nil
		}
	}

	subSystem, err := core.LoadSubSystem(subSystemName, false)
	if err != nil {
		return err
	}

	err = subSystem.Reset()
	if err != nil {
		return err
	}

	cmdr.Success.Printfln(apx.Trans("subsystems.reset.info.success"), subSystemName)

	return nil
}

bufio import

Import example:

import "bufio"

encoding/json import

Import example:

import "encoding/json"

fmt import

Import example:

import "fmt"

os import

Import example:

import "os"

strings import

Import example:

import "strings"

github.com/spf13/cobra import

Import example:

import "github.com/spf13/cobra"

github.com/vanilla-os/apx/v2/core import

Import example:

import "github.com/vanilla-os/apx/v2/core"

github.com/vanilla-os/orchid/cmdr import

Import example:

import "github.com/vanilla-os/orchid/cmdr"

embed import

Import example:

import "embed"

github.com/vanilla-os/orchid/cmdr import

Import example:

import "github.com/vanilla-os/orchid/cmdr"

fmt import

Import example:

import "fmt"

slices import

Import example:

import "slices"

github.com/spf13/cobra import

Import example:

import "github.com/spf13/cobra"

github.com/vanilla-os/apx/v2/core import

Import example:

import "github.com/vanilla-os/apx/v2/core"

github.com/vanilla-os/orchid/cmdr import

Import example:

import "github.com/vanilla-os/orchid/cmdr"

bufio import

Import example:

import "bufio"

encoding/json import

Import example:

import "encoding/json"

fmt import

Import example:

import "fmt"

os import

Import example:

import "os"

strings import

Import example:

import "strings"

github.com/spf13/cobra import

Import example:

import "github.com/spf13/cobra"

github.com/vanilla-os/apx/v2/core import

Import example:

import "github.com/vanilla-os/apx/v2/core"

github.com/vanilla-os/orchid/cmdr import

Import example:

import "github.com/vanilla-os/orchid/cmdr"

encoding/json import

Import example:

import "encoding/json"

fmt import

Import example:

import "fmt"

os import

Import example:

import "os"

strings import

Import example:

import "strings"

github.com/spf13/cobra import

Import example:

import "github.com/spf13/cobra"

github.com/vanilla-os/apx/v2/core import

Import example:

import "github.com/vanilla-os/apx/v2/core"

github.com/vanilla-os/orchid/cmdr import

Import example:

import "github.com/vanilla-os/orchid/cmdr"