cli.ts 10.0 KB
Newer Older
A
Anmol Sethi 已提交
1 2
import * as fs from "fs-extra"
import yaml from "js-yaml"
3
import * as path from "path"
A
Asher 已提交
4
import { field, logger, Level } from "@coder/logger"
5
import { Args as VsArgs } from "../../lib/vscode/src/vs/server/ipc"
6
import { AuthType } from "./http"
7
import { paths, uxPath } from "./util"
8

A
Asher 已提交
9 10 11 12
export class Optional<T> {
  public constructor(public readonly value?: T) {}
}

A
Asher 已提交
13 14 15 16 17 18 19 20
export enum LogLevel {
  Trace = "trace",
  Debug = "debug",
  Info = "info",
  Warn = "warn",
  Error = "error",
}

A
Asher 已提交
21 22
export class OptionalString extends Optional<string> {}

23
export interface Args extends VsArgs {
A
Anmol Sethi 已提交
24
  readonly config?: string
A
Asher 已提交
25 26 27
  readonly auth?: AuthType
  readonly cert?: OptionalString
  readonly "cert-key"?: string
A
Asher 已提交
28
  readonly "disable-updates"?: boolean
A
Asher 已提交
29
  readonly "disable-telemetry"?: boolean
A
Asher 已提交
30 31 32
  readonly help?: boolean
  readonly host?: string
  readonly json?: boolean
A
Asher 已提交
33
  log?: LogLevel
A
Asher 已提交
34 35
  readonly open?: boolean
  readonly port?: number
36
  readonly "bind-addr"?: string
A
Asher 已提交
37 38
  readonly socket?: string
  readonly version?: boolean
A
Asher 已提交
39
  readonly force?: boolean
A
Asher 已提交
40 41
  readonly "list-extensions"?: boolean
  readonly "install-extension"?: string[]
42
  readonly "show-versions"?: boolean
A
Asher 已提交
43
  readonly "uninstall-extension"?: string[]
A
Asher 已提交
44
  readonly "proxy-domain"?: string[]
A
Anmol Sethi 已提交
45
  readonly locale?: string
A
Asher 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  readonly _: string[]
}

interface Option<T> {
  type: T
  /**
   * Short flag for the option.
   */
  short?: string
  /**
   * Whether the option is a path and should be resolved.
   */
  path?: boolean
  /**
   * Description of the option. Leave blank to hide the option.
   */
  description?: string
}

type OptionType<T> = T extends boolean
  ? "boolean"
  : T extends OptionalString
  ? typeof OptionalString
A
Asher 已提交
69 70
  : T extends LogLevel
  ? typeof LogLevel
A
Asher 已提交
71 72 73 74 75 76 77 78 79 80 81 82
  : T extends AuthType
  ? typeof AuthType
  : T extends number
  ? "number"
  : T extends string
  ? "string"
  : T extends string[]
  ? "string[]"
  : "unknown"

type Options<T> = {
  [P in keyof T]: Option<OptionType<T[P]>>
83 84
}

A
Asher 已提交
85 86 87 88 89 90 91 92
const options: Options<Required<Args>> = {
  auth: { type: AuthType, description: "The type of authentication to use." },
  cert: {
    type: OptionalString,
    path: true,
    description: "Path to certificate. Generated if no path is provided.",
  },
  "cert-key": { type: "string", path: true, description: "Path to certificate key when using non-generated cert." },
A
Asher 已提交
93
  "disable-updates": { type: "boolean", description: "Disable automatic updates." },
A
Asher 已提交
94
  "disable-telemetry": { type: "boolean", description: "Disable telemetry." },
A
Asher 已提交
95 96
  help: { type: "boolean", short: "h", description: "Show this output." },
  json: { type: "boolean" },
A
Asher 已提交
97
  open: { type: "boolean", description: "Open in browser on startup. Does not work remotely." },
98 99 100

  "bind-addr": { type: "string", description: "Address to bind to in host:port." },

A
Anmol Sethi 已提交
101 102
  config: { type: "string", description: "Path to yaml config file." },

103 104 105 106
  // These two have been deprecated by bindAddr.
  host: { type: "string", description: "" },
  port: { type: "number", description: "" },

A
Anmol Sethi 已提交
107
  socket: { type: "string", path: true, description: "Path to a socket (bind-addr will be ignored)." },
A
Asher 已提交
108 109 110 111 112 113 114 115
  version: { type: "boolean", short: "v", description: "Display version information." },
  _: { type: "string[]" },

  "user-data-dir": { type: "string", path: true, description: "Path to the user data directory." },
  "extensions-dir": { type: "string", path: true, description: "Path to the extensions directory." },
  "builtin-extensions-dir": { type: "string", path: true },
  "extra-extensions-dir": { type: "string[]", path: true },
  "extra-builtin-extensions-dir": { type: "string[]", path: true },
116 117 118 119 120
  "list-extensions": { type: "boolean", description: "List installed VS Code extensions." },
  force: { type: "boolean", description: "Avoid prompts when installing VS Code extensions." },
  "install-extension": { type: "string[]", description: "Install or update a VS Code extension by id or vsix." },
  "uninstall-extension": { type: "string[]", description: "Uninstall a VS Code extension by id." },
  "show-versions": { type: "boolean", description: "Show VS Code extension versions." },
A
Asher 已提交
121
  "proxy-domain": { type: "string[]", description: "Domain used for proxying ports." },
A
Asher 已提交
122

A
Asher 已提交
123
  locale: { type: "string" },
A
Asher 已提交
124
  log: { type: LogLevel },
A
Asher 已提交
125 126 127 128 129 130 131 132 133 134
  verbose: { type: "boolean", short: "vvv", description: "Enable verbose logging." },
}

export const optionDescriptions = (): string[] => {
  const entries = Object.entries(options).filter(([, v]) => !!v.description)
  const widths = entries.reduce(
    (prev, [k, v]) => ({
      long: k.length > prev.long ? k.length : prev.long,
      short: v.short && v.short.length > prev.short ? v.short.length : prev.short,
    }),
A
Anmol Sethi 已提交
135
    { short: 0, long: 0 },
A
Asher 已提交
136 137 138 139
  )
  return entries.map(
    ([k, v]) =>
      `${" ".repeat(widths.short - (v.short ? v.short.length : 0))}${v.short ? `-${v.short}` : " "} --${k}${" ".repeat(
A
Anmol Sethi 已提交
140 141
        widths.long - k.length,
      )} ${v.description}${typeof v.type === "object" ? ` [${Object.values(v.type).join(", ")}]` : ""}`,
A
Asher 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  )
}

export const parse = (argv: string[]): Args => {
  const args: Args = { _: [] }
  let ended = false

  for (let i = 0; i < argv.length; ++i) {
    const arg = argv[i]

    // -- signals the end of option parsing.
    if (!ended && arg == "--") {
      ended = true
      continue
    }
157

A
Asher 已提交
158 159 160
    // Options start with a dash and require a value if non-boolean.
    if (!ended && arg.startsWith("-")) {
      let key: keyof Args | undefined
161
      let value: string | undefined
A
Asher 已提交
162
      if (arg.startsWith("--")) {
163 164 165
        const split = arg.replace(/^--/, "").split("=", 2)
        key = split[0] as keyof Args
        value = split[1]
A
Asher 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      } else {
        const short = arg.replace(/^-/, "")
        const pair = Object.entries(options).find(([, v]) => v.short === short)
        if (pair) {
          key = pair[0] as keyof Args
        }
      }

      if (!key || !options[key]) {
        throw new Error(`Unknown option ${arg}`)
      }

      const option = options[key]
      if (option.type === "boolean") {
        ;(args[key] as boolean) = true
        continue
      }

184 185 186 187 188 189
      // Might already have a value if it was the --long=value format.
      if (typeof value === "undefined") {
        // A value is only valid if it doesn't look like an option.
        value = argv[i + 1] && !argv[i + 1].startsWith("-") ? argv[++i] : undefined
      }

A
Asher 已提交
190 191 192 193
      if (!value && option.type === OptionalString) {
        ;(args[key] as OptionalString) = new OptionalString(value)
        continue
      } else if (!value) {
194
        throw new Error(`--${key} requires a value`)
A
Asher 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
      }

      if (option.path) {
        value = path.resolve(value)
      }

      switch (option.type) {
        case "string":
          ;(args[key] as string) = value
          break
        case "string[]":
          if (!args[key]) {
            ;(args[key] as string[]) = []
          }
          ;(args[key] as string[]).push(value)
          break
        case "number":
          ;(args[key] as number) = parseInt(value, 10)
          if (isNaN(args[key] as number)) {
214
            throw new Error(`--${key} must be a number`)
A
Asher 已提交
215 216 217 218 219 220
          }
          break
        case OptionalString:
          ;(args[key] as OptionalString) = new OptionalString(value)
          break
        default: {
221
          if (!Object.values(option.type).includes(value)) {
222
            throw new Error(`--${key} valid values: [${Object.values(option.type).join(", ")}]`)
A
Asher 已提交
223 224 225 226 227 228 229 230 231 232 233
          }
          ;(args[key] as string) = value
          break
        }
      }

      continue
    }

    // Everything else goes into _.
    args._.push(arg)
234 235
  }

A
Asher 已提交
236 237
  logger.debug("parsed command line", field("args", args))

238 239 240 241 242 243 244 245 246
  // --verbose takes priority over --log and --log takes priority over the
  // environment variable.
  if (args.verbose) {
    args.log = LogLevel.Trace
  } else if (
    !args.log &&
    process.env.LOG_LEVEL &&
    Object.values(LogLevel).includes(process.env.LOG_LEVEL as LogLevel)
  ) {
A
Asher 已提交
247
    args.log = process.env.LOG_LEVEL as LogLevel
248
  }
A
Asher 已提交
249

250 251 252 253
  // Sync --log, --verbose, the environment variable, and logger level.
  if (args.log) {
    process.env.LOG_LEVEL = args.log
  }
A
Asher 已提交
254
  switch (args.log) {
A
Asher 已提交
255
    case LogLevel.Trace:
A
Asher 已提交
256
      logger.level = Level.Trace
257
      args.verbose = true
A
Asher 已提交
258
      break
A
Asher 已提交
259
    case LogLevel.Debug:
A
Asher 已提交
260 261
      logger.level = Level.Debug
      break
A
Asher 已提交
262
    case LogLevel.Info:
A
Asher 已提交
263 264
      logger.level = Level.Info
      break
A
Asher 已提交
265
    case LogLevel.Warn:
A
Asher 已提交
266 267
      logger.level = Level.Warning
      break
A
Asher 已提交
268
    case LogLevel.Error:
A
Asher 已提交
269 270 271 272 273
      logger.level = Level.Error
      break
  }

  if (!args["user-data-dir"]) {
274
    args["user-data-dir"] = paths.data
A
Asher 已提交
275 276 277 278 279 280 281
  }

  if (!args["extensions-dir"]) {
    args["extensions-dir"] = path.join(args["user-data-dir"], "extensions")
  }

  return args
282
}
A
Anmol Sethi 已提交
283

284 285 286 287 288
const defaultConfigFile = `
auth: password
bind-addr: 127.0.0.1:8080
`.trimLeft()

A
Anmol Sethi 已提交
289 290 291 292 293 294 295 296 297 298 299
// readConfigFile reads the config file specified in the config flag
// and loads it's configuration.
//
// Flags set on the CLI take priority.
//
// The config file can also be passed via $CODE_SERVER_CONFIG and defaults
// to ~/.config/code-server/config.yaml.
export async function readConfigFile(args: Args): Promise<Args> {
  const configPath = getConfigPath(args)

  if (!(await fs.pathExists(configPath))) {
300 301
    await fs.outputFile(configPath, defaultConfigFile)
    logger.info(`Wrote default config file to ${uxPath(configPath)}`)
A
Anmol Sethi 已提交
302 303
  }

304 305
  logger.info(`Using config file from ${uxPath(configPath)}`)

A
Anmol Sethi 已提交
306 307 308 309 310 311 312
  const configFile = await fs.readFile(configPath)
  const config = yaml.safeLoad(configFile.toString(), {
    filename: args.config,
  })

  // We convert the config file into a set of flags.
  // This is a temporary measure until we add a proper CLI library.
313 314 315 316 317 318
  const configFileArgv = Object.entries(config).map(([optName, opt]) => {
    if (opt === null) {
      return `--${optName}`
    }
    return `--${optName}=${opt}`
  })
A
Anmol Sethi 已提交
319 320 321 322 323 324
  const configFileArgs = parse(configFileArgv)

  // This prioritizes the flags set in args over the ones in the config file.
  return Object.assign(configFileArgs, args)
}

325
function getConfigPath(args: Args): string {
A
Anmol Sethi 已提交
326 327 328 329 330 331
  if (args.config !== undefined) {
    return args.config
  }
  if (process.env.CODE_SERVER_CONFIG !== undefined) {
    return process.env.CODE_SERVER_CONFIG
  }
332
  return path.join(paths.config, "config.yaml")
A
Anmol Sethi 已提交
333
}