Skip to content

Commit

Permalink
Flatten service base argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed May 11, 2023
1 parent 96e371e commit 6dbac5e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 46 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ title: "11. Changelog"

All notable changes to this project will be documented in this section.

## [1.0.0-beta.23] - 2023-05-11

- Reduce `pup service install/uninstall/generate` to `pup install/uninstall [--dry-run]`

## [1.0.0-beta.22] - 2023-05-10

- Make `--` optional for `--help`, `--version` and `--upgrade`
Expand Down
13 changes: 7 additions & 6 deletions docs/service.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,37 @@ Replace `username` with your actual username.

2. Install Pup as a user mode service, named `pup`:

`pup service install`
`pup install`

To install multiple services, provide a unique name for each instance:

`pup service install --name my-service`
`pup install --name my-service`

### System Mode Installation

**Note**: This method works for all service managers, but may include some manual steps, and will require privileged access (e.g. sudo).

1. Install Pup as a system service, by default named `pup`:

`pup service install --system`
`pup install --system`

To install multiple services, provide a unique name for each instance:

`pup service install --system --name my-service`
`pup install --system --name my-service`

2. Follow the on-screen instructions to copy the generated configuration file to the correct location, and enable the service.

### Service Argument Reference

Use the `pup service <method> [...flags]` command with the following methods and flags:
Use the `pup <method> [...flags]` command with the following methods and flags:

- Methods:
- `install`: Installs the configured Pup instance as a system service, then verifies the installation by enabling and starting the service. Rolls back any changes on error.
- `generate`: Generates the configuration and prints it to stdout along with a suitable path. Makes no changes to the system.
- `uninstall`: Uninstall service

- Flags:
- `--config`: Specifies the configuration file for the instance to be installed, defaulting to `pup.json` or `pup.jsonc` in the current directory.
- `--dry-run`: Generates the configuration and prints it to stdout along with a suitable path. Makes no changes to the system.
- `--name`: Sets the service name, defaulting to `pup`.
- `--system`: Installs the service at the system level, with the default being user level.
- `--home`: Specifies a home directory, defaulting to the current user's $HOME.
Expand Down
7 changes: 5 additions & 2 deletions lib/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function parseArguments(args: string[]): Args {
"version",
"help",
"autostart",

"dry-run",
]

// All string arguments
Expand Down Expand Up @@ -89,7 +91,8 @@ function checkArguments(args: Args): Args {
"block",
"unblock",
"run",
"service",
"install",
"uninstall",
"logs",
// Aliases for --equivalent
"upgrade",
Expand Down Expand Up @@ -164,7 +167,7 @@ function checkArguments(args: Args): Args {
}

// Ensure --env flag can only be used with 'service install' base argument
if (args.env && (baseArgument !== "service" || args._[1] !== "install")) {
if (args.env && (baseArgument !== "install")) {
throw new Error("Argument '--env' can only be used with 'service install' base argument")
}

Expand Down
65 changes: 30 additions & 35 deletions lib/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,44 +107,39 @@ async function main(inputArgs: string[]) {
/**
* Handle the install argument
*/
if (baseArgument === "service") {
if (secondaryBaseArgument === "install" || secondaryBaseArgument === "generate") {
if (!configFile) {
console.error("Service maintenance commands require pup to run with a configuration file, exiting.")
Deno.exit(1)
}
if (baseArgument === "install") {
if (!configFile) {
console.error("Service maintenance commands require pup to run with a configuration file, exiting.")
Deno.exit(1)
}

const system = args.system
const name = args.name || "pup"
const config = args.config
const cwd = args.cwd
const cmd = `pup run ${config ? `--config ${config}` : ""}`
const user = args.user
const home = args.home
const env = args.env || []
const system = args.system
const name = args.name || "pup"
const config = args.config
const cwd = args.cwd
const cmd = `pup run ${config ? `--config ${config}` : ""}`
const user = args.user
const home = args.home
const env = args.env || []

try {
await installService({ system, name, cmd, cwd, user, home, env }, secondaryBaseArgument === "generate")
Deno.exit(0)
} catch (e) {
console.error(`Could not install service, error: ${e.message}`)
Deno.exit(1)
}
} else if (secondaryBaseArgument === "uninstall") {
const system = args.system
const name = args.name || "pup"
const home = args.home
try {
await installService({ system, name, cmd, cwd, user, home, env }, args["dry-run"])
Deno.exit(0)
} catch (e) {
console.error(`Could not install service, error: ${e.message}`)
Deno.exit(1)
}
} else if (secondaryBaseArgument === "uninstall") {
const system = args.system
const name = args.name || "pup"
const home = args.home

try {
await uninstallService({ system, name, home })
console.log(`Service '${name}' uninstalled.`)
Deno.exit(0)
} catch (e) {
console.error(`Could not uninstall service, error: ${e.message}`)
Deno.exit(1)
}
} else {
console.error(`Unknown service maintenance command '${secondaryBaseArgument}', exiting.`)
try {
await uninstallService({ system, name, home })
console.log(`Service '${name}' uninstalled.`)
Deno.exit(0)
} catch (e) {
console.error(`Could not uninstall service, error: ${e.message}`)
Deno.exit(1)
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/cli/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ export function printFlags() {
{ separator: "empty" },
{ content: "Service installation", spanStart: 1 },
{ separator: "empty" },
{ long: "service install", description: "Install pup as a service" },
{ long: "service generate", description: "Generate and output service configuration, do not install" },
{ long: "install", description: "Install pup instance as a service" },
{ long: "uninstall", description: "Uninstall service" },
{ separator: "empty" },
{ short: "-c", long: '--config "path"', description: "Optional. Use specific configuration file." },
{ description: "Default: ./pup.jsonc or ./pup.json" },
{ long: "--dry-run", description: "Generate and output service configuration, do not actually install the service" },
{ long: "--system", description: "Optional. Install the service system-wide (requires root)." },
{ long: "--cwd", description: "Optional. Set working directory for service" },
{ long: "--name", description: "Optional. Set service name" },
Expand Down
6 changes: 5 additions & 1 deletion test/cli/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Deno.test("Boolean options and aliases are parsed correctly", () => {

help: true,
h: true,
"dry-run": false,

autostart: true,
A: true,
Expand Down Expand Up @@ -55,6 +56,7 @@ Deno.test("String options and aliases are parsed correctly", () => {
"cron",
"--terminate",
"terminate",
"--dry-run",
]
const parsedArgs = parseArguments(inputArgs)
const expectedArgs = {
Expand Down Expand Up @@ -91,6 +93,7 @@ Deno.test("String options and aliases are parsed correctly", () => {
v: false,
help: false,
h: false,
"dry-run": true,

_: [],
"--": [],
Expand Down Expand Up @@ -266,7 +269,7 @@ Deno.test("checkArguments should throw error when --env argument is provided wit

Deno.test("checkArguments should return the provided arguments when service install and --env are used together", () => {
const expectedArgs = {
_: ["service", "install"],
_: ["install"],
env: "NODE_ENV=production",
}
const result = checkArguments(expectedArgs)
Expand All @@ -292,6 +295,7 @@ Deno.test("Collect env arguments formatted as KEY=VALUE", () => {
h: false,
autostart: false,
A: false,
"dry-run": false,

/* Unspecified string options will not be included */
_: [],
Expand Down

0 comments on commit 6dbac5e

Please sign in to comment.