diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a989748..65821b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ on: jobs: build: name: build - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 strategy: fail-fast: true env: @@ -22,7 +22,7 @@ jobs: PACKAGECLOUD_TOKEN: ${{ secrets.PACKAGECLOUD_TOKEN }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: make version run: | make version .env.docker @@ -31,7 +31,7 @@ jobs: - run: make build-in-docker - run: make validate-in-docker - name: upload packages - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: build path: build/**/* diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 49b45a3..432591e 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -26,15 +26,15 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} - name: Autobuild - uses: github/codeql-action/autobuild@v2 + uses: github/codeql-action/autobuild@v3 - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 04d8686..9511f14 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,20 +13,20 @@ on: jobs: hadolint: name: hadolint - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - name: Clone - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Run hadolint uses: hadolint/hadolint-action@54c9adbab1582c2ef04b2016b760714a4bfde3cf # v1.5.0 => c27bd9edc1e95eed30474db8f295ff5807ebca14 markdown-lint: name: markdown-lint - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - name: Clone - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Run markdown-lint uses: avto-dev/markdown-lint@04d43ee9191307b50935a753da3b775ab695eceb # v1.5.0 => 04d43ee9191307b50935a753da3b775ab695eceb @@ -36,10 +36,10 @@ jobs: shellcheck: name: shellcheck - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - name: Clone - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Run shellcheck uses: ludeeus/action-shellcheck@00cae500b08a931fb5698e11e79bfbd38e612a38 # 1.1.0 => 94e0aab03ca135d11a35e5bfc14e6746dc56e7e9 @@ -47,12 +47,12 @@ jobs: SHELLCHECK_OPTS: -s bash shfmt: name: shfmt - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - name: Clone - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Run shfmt - uses: luizm/action-sh-checker@76ab0b22e1f194e4a582edc7969df6485c4e9246 + uses: luizm/action-sh-checker@c6edb3de93e904488b413636d96c6a56e3ad671a # v0.3.0 => 7f44869033b40ee4ffe7dc76c87a1bc66e3d025a env: GITHUB_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }} @@ -63,10 +63,10 @@ jobs: yamllint: name: yamllint - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - name: Clone - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Run yamllint uses: ibiqlik/action-yamllint@2576378a8e339169678f9939646ee3ee325e845c # v3.0.2 => c19bd0523a9011c3a3960fe6640a0882b59af15d diff --git a/Dockerfile b/Dockerfile index 0e971fc..fa41973 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20.7-bullseye +FROM golang:1.22.0-bookworm # hadolint ignore=DL3027 RUN apt-get update \ diff --git a/Makefile b/Makefile index 8daa292..e09f834 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ MAINTAINER_NAME = Jose Diaz-Gonzalez REPOSITORY = procfile-util HARDWARE = $(shell uname -m) SYSTEM_NAME = $(shell uname -s | tr '[:upper:]' '[:lower:]') -BASE_VERSION ?= 0.16.0 +BASE_VERSION ?= 0.17.0 IMAGE_NAME ?= $(MAINTAINER)/$(REPOSITORY) PACKAGECLOUD_REPOSITORY ?= dokku/dokku-betafish @@ -47,10 +47,8 @@ build: prebuild @$(MAKE) build/darwin/$(NAME)-arm64 @$(MAKE) build/linux/$(NAME)-amd64 @$(MAKE) build/linux/$(NAME)-arm64 - @$(MAKE) build/linux/$(NAME)-armhf @$(MAKE) build/deb/$(NAME)_$(VERSION)_amd64.deb @$(MAKE) build/deb/$(NAME)_$(VERSION)_arm64.deb - @$(MAKE) build/deb/$(NAME)_$(VERSION)_armhf.deb build-docker-image: docker build --rm -q -f Dockerfile -t $(IMAGE_NAME):build . @@ -89,12 +87,6 @@ build/linux/$(NAME)-arm64: -ldflags "-s -w -X main.Version=$(VERSION)" \ -o build/linux/$(NAME)-arm64 -build/linux/$(NAME)-armhf: - mkdir -p build/linux - CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=5 go build -a -asmflags=-trimpath=/src -gcflags=-trimpath=/src \ - -ldflags "-s -w -X main.Version=$(VERSION)" \ - -o build/linux/$(NAME)-armhf - build/deb/$(NAME)_$(VERSION)_amd64.deb: build/linux/$(NAME)-amd64 export SOURCE_DATE_EPOCH=$(shell git log -1 --format=%ct) \ && mkdir -p build/deb \ @@ -135,26 +127,6 @@ build/deb/$(NAME)_$(VERSION)_arm64.deb: build/linux/$(NAME)-arm64 build/linux/$(NAME)-arm64=/usr/bin/$(NAME) \ LICENSE=/usr/share/doc/$(NAME)/copyright -build/deb/$(NAME)_$(VERSION)_armhf.deb: build/linux/$(NAME)-armhf - export SOURCE_DATE_EPOCH=$(shell git log -1 --format=%ct) \ - && mkdir -p build/deb \ - && fpm \ - --architecture armhf \ - --category utils \ - --description "$$PACKAGE_DESCRIPTION" \ - --input-type dir \ - --license 'MIT License' \ - --maintainer "$(MAINTAINER_NAME) <$(EMAIL)>" \ - --name $(NAME) \ - --output-type deb \ - --package build/deb/$(NAME)_$(VERSION)_armhf.deb \ - --url "https://github.com/$(MAINTAINER)/$(REPOSITORY)" \ - --vendor "" \ - --version $(VERSION) \ - --verbose \ - build/linux/$(NAME)-armhf=/usr/bin/$(NAME) \ - LICENSE=/usr/share/doc/$(NAME)/copyright - clean: rm -rf build release validation @@ -180,52 +152,39 @@ release: build bin/gh-release bin/gh-release-body rm -rf release && mkdir release tar -zcf release/$(NAME)_$(VERSION)_linux_amd64.tgz -C build/linux $(NAME)-amd64 tar -zcf release/$(NAME)_$(VERSION)_linux_arm64.tgz -C build/linux $(NAME)-arm64 - tar -zcf release/$(NAME)_$(VERSION)_linux_armhf.tgz -C build/linux $(NAME)-armhf tar -zcf release/$(NAME)_$(VERSION)_darwin_amd64.tgz -C build/darwin $(NAME)-amd64 tar -zcf release/$(NAME)_$(VERSION)_darwin_arm64.tgz -C build/darwin $(NAME)-arm64 cp build/deb/$(NAME)_$(VERSION)_amd64.deb release/$(NAME)_$(VERSION)_amd64.deb cp build/deb/$(NAME)_$(VERSION)_arm64.deb release/$(NAME)_$(VERSION)_arm64.deb - cp build/deb/$(NAME)_$(VERSION)_armhf.deb release/$(NAME)_$(VERSION)_armhf.deb bin/gh-release create $(MAINTAINER)/$(REPOSITORY) $(VERSION) $(shell git rev-parse --abbrev-ref HEAD) bin/gh-release-body $(MAINTAINER)/$(REPOSITORY) v$(VERSION) release-packagecloud: @$(MAKE) release-packagecloud-deb -release-packagecloud-deb: build/deb/$(NAME)_$(VERSION)_amd64.deb build/deb/$(NAME)_$(VERSION)_arm64.deb build/deb/$(NAME)_$(VERSION)_armhf.deb - package_cloud push $(PACKAGECLOUD_REPOSITORY)/ubuntu/bionic build/deb/$(NAME)_$(VERSION)_amd64.deb +release-packagecloud-deb: build/deb/$(NAME)_$(VERSION)_amd64.deb build/deb/$(NAME)_$(VERSION)_arm64.deb package_cloud push $(PACKAGECLOUD_REPOSITORY)/ubuntu/focal build/deb/$(NAME)_$(VERSION)_amd64.deb package_cloud push $(PACKAGECLOUD_REPOSITORY)/ubuntu/jammy build/deb/$(NAME)_$(VERSION)_amd64.deb - package_cloud push $(PACKAGECLOUD_REPOSITORY)/debian/buster build/deb/$(NAME)_$(VERSION)_amd64.deb package_cloud push $(PACKAGECLOUD_REPOSITORY)/debian/bullseye build/deb/$(NAME)_$(VERSION)_amd64.deb package_cloud push $(PACKAGECLOUD_REPOSITORY)/debian/bookworm build/deb/$(NAME)_$(VERSION)_amd64.deb package_cloud push $(PACKAGECLOUD_REPOSITORY)/ubuntu/focal build/deb/$(NAME)_$(VERSION)_arm64.deb package_cloud push $(PACKAGECLOUD_REPOSITORY)/ubuntu/jammy build/deb/$(NAME)_$(VERSION)_arm64.deb package_cloud push $(PACKAGECLOUD_REPOSITORY)/debian/bullseye build/deb/$(NAME)_$(VERSION)_arm64.deb package_cloud push $(PACKAGECLOUD_REPOSITORY)/debian/bookworm build/deb/$(NAME)_$(VERSION)_arm64.deb - package_cloud push $(PACKAGECLOUD_REPOSITORY)/ubuntu/focal build/deb/$(NAME)_$(VERSION)_armhf.deb - package_cloud push $(PACKAGECLOUD_REPOSITORY)/ubuntu/jammy build/deb/$(NAME)_$(VERSION)_armhf.deb - package_cloud push $(PACKAGECLOUD_REPOSITORY)/raspbian/buster build/deb/$(NAME)_$(VERSION)_armhf.deb - package_cloud push $(PACKAGECLOUD_REPOSITORY)/raspbian/bullseye build/deb/$(NAME)_$(VERSION)_armhf.deb validate: mkdir -p validation lintian build/deb/$(NAME)_$(VERSION)_amd64.deb || true lintian build/deb/$(NAME)_$(VERSION)_arm64.deb || true - lintian build/deb/$(NAME)_$(VERSION)_armhf.deb || true dpkg-deb --info build/deb/$(NAME)_$(VERSION)_amd64.deb dpkg-deb --info build/deb/$(NAME)_$(VERSION)_arm64.deb - dpkg-deb --info build/deb/$(NAME)_$(VERSION)_armhf.deb dpkg -c build/deb/$(NAME)_$(VERSION)_amd64.deb dpkg -c build/deb/$(NAME)_$(VERSION)_arm64.deb - dpkg -c build/deb/$(NAME)_$(VERSION)_armhf.deb cd validation && ar -x ../build/deb/$(NAME)_$(VERSION)_amd64.deb cd validation && ar -x ../build/deb/$(NAME)_$(VERSION)_arm64.deb - cd validation && ar -x ../build/deb/$(NAME)_$(VERSION)_armhf.deb ls -lah build/deb validation sha1sum build/deb/$(NAME)_$(VERSION)_amd64.deb sha1sum build/deb/$(NAME)_$(VERSION)_arm64.deb - sha1sum build/deb/$(NAME)_$(VERSION)_armhf.deb bats test.bats prebuild: diff --git a/PROCFILE_FORMAT.md b/PROCFILE_FORMAT.md index 0fa3dda..6a2a972 100644 --- a/PROCFILE_FORMAT.md +++ b/PROCFILE_FORMAT.md @@ -6,9 +6,9 @@ A Procfile is a file that was [promoted by Heroku](https://blog.heroku.com/the_n The `procfile-util` tool expects a Procfile to be defined as one or more lines containing one of: -- a comment (preceeded by a `#` symbol) +- a comment (preceeded by a `#` symbol or two `//` characters) - a process-type/command combination (with optional trailing whitespace or trailing comment) - - when there is a trailing comment, the `#` symbol _must_ be preceeded by one or more `whitespace` characters. + - when there is a trailing comment, the `#` symbol/`//` characters _must_ be preceeded by one or more `whitespace` characters. - a blank line (with optional trailing whitespace) Comments and blank lines are ignored, while process-type/command combinations look like the following: diff --git a/commands/check.go b/commands/check.go new file mode 100644 index 0000000..594cfcf --- /dev/null +++ b/commands/check.go @@ -0,0 +1,100 @@ +package commands + +import ( + "fmt" + "os" + "strings" + + "github.com/josegonzalez/cli-skeleton/command" + "github.com/posener/complete" + flag "github.com/spf13/pflag" +) + +type CheckCommand struct { + command.Meta + GlobalFlagCommand +} + +func (c *CheckCommand) Name() string { + return "check" +} + +func (c *CheckCommand) Synopsis() string { + return "Eats one or more lollipops" +} + +func (c *CheckCommand) Help() string { + return command.CommandHelp(c) +} + +func (c *CheckCommand) Examples() map[string]string { + appName := os.Getenv("CLI_APP_NAME") + return map[string]string{ + "Check if the procfile is valid": fmt.Sprintf("%s %s", appName, c.Name()), + } +} + +func (c *CheckCommand) Arguments() []command.Argument { + args := []command.Argument{} + return args +} + +func (c *CheckCommand) AutocompleteArgs() complete.Predictor { + return complete.PredictNothing +} + +func (c *CheckCommand) ParsedArguments(args []string) (map[string]command.Argument, error) { + return command.ParseArguments(args, c.Arguments()) +} + +func (c *CheckCommand) FlagSet() *flag.FlagSet { + f := c.Meta.FlagSet(c.Name(), command.FlagSetClient) + c.GlobalFlags(f) + return f +} + +func (c *CheckCommand) AutocompleteFlags() complete.Flags { + return command.MergeAutocompleteFlags( + c.Meta.AutocompleteFlags(command.FlagSetClient), + c.AutocompleteGlobalFlags(), + complete.Flags{}, + ) +} + +func (c *CheckCommand) Run(args []string) int { + flags := c.FlagSet() + flags.Usage = func() { c.Ui.Output(c.Help()) } + if err := flags.Parse(args); err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + _, err := c.ParsedArguments(flags.Args()) + if err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + entries, err := parseProcfile(c.procfile, c.delimiter, c.strict) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if len(entries) == 0 { + c.Ui.Error("No processes defined") + return 1 + } + + names := []string{} + for _, entry := range entries { + names = append(names, entry.Name) + } + + processNames := strings.Join(names[:], ", ") + c.Ui.Output(fmt.Sprintf("valid procfile detected %v", processNames)) + + return 0 +} diff --git a/commands/check_command.go b/commands/check_command.go deleted file mode 100644 index bc80d1f..0000000 --- a/commands/check_command.go +++ /dev/null @@ -1,26 +0,0 @@ -package commands - -import ( - "fmt" - "os" - "strings" - - "procfile-util/procfile" -) - - -func CheckCommand(entries []procfile.ProcfileEntry) bool { - if len(entries) == 0 { - fmt.Fprintf(os.Stderr, "no processes defined\n") - return false - } - - names := []string{} - for _, entry := range entries { - names = append(names, entry.Name) - } - - processNames := strings.Join(names[:], ", ") - fmt.Printf("valid procfile detected %v\n", processNames) - return true -} diff --git a/commands/delete.go b/commands/delete.go new file mode 100644 index 0000000..fbef53b --- /dev/null +++ b/commands/delete.go @@ -0,0 +1,117 @@ +package commands + +import ( + "fmt" + "os" + "procfile-util/procfile" + + "github.com/josegonzalez/cli-skeleton/command" + "github.com/posener/complete" + flag "github.com/spf13/pflag" +) + +type DeleteCommand struct { + command.Meta + GlobalFlagCommand + + processType string + stdout bool + writePath string +} + +func (c *DeleteCommand) Name() string { + return "delete" +} + +func (c *DeleteCommand) Synopsis() string { + return "Eats one or more lollipops" +} + +func (c *DeleteCommand) Help() string { + return command.CommandHelp(c) +} + +func (c *DeleteCommand) Examples() map[string]string { + appName := os.Getenv("CLI_APP_NAME") + return map[string]string{ + "Command": fmt.Sprintf("%s %s", appName, c.Name()), + } +} + +func (c *DeleteCommand) Arguments() []command.Argument { + args := []command.Argument{} + return args +} + +func (c *DeleteCommand) AutocompleteArgs() complete.Predictor { + return complete.PredictNothing +} + +func (c *DeleteCommand) ParsedArguments(args []string) (map[string]command.Argument, error) { + return command.ParseArguments(args, c.Arguments()) +} + +func (c *DeleteCommand) FlagSet() *flag.FlagSet { + f := c.Meta.FlagSet(c.Name(), command.FlagSetClient) + // required? + f.StringVarP(&c.processType, "process-type", "p", "", "name of process to delete") + f.BoolVarP(&c.stdout, "stdout", "s", false, "write output to stdout") + f.StringVarP(&c.writePath, "write-path", "w", "", "path to Procfile to write to") + c.GlobalFlags(f) + return f +} + +func (c *DeleteCommand) AutocompleteFlags() complete.Flags { + return command.MergeAutocompleteFlags( + c.Meta.AutocompleteFlags(command.FlagSetClient), + c.AutocompleteGlobalFlags(), + complete.Flags{ + "--process-type": complete.PredictAnything, + "--stdout": complete.PredictNothing, + "--write-path": complete.PredictAnything, + }, + ) +} + +func (c *DeleteCommand) Run(args []string) int { + flags := c.FlagSet() + flags.Usage = func() { c.Ui.Output(c.Help()) } + if err := flags.Parse(args); err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + _, err := c.ParsedArguments(flags.Args()) + if err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + entries, err := parseProcfile(c.procfile, c.delimiter, c.strict) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if len(entries) == 0 { + c.Ui.Error("No processes defined") + return 1 + } + + var validEntries []procfile.ProcfileEntry + for _, entry := range entries { + if c.processType == entry.Name { + continue + } + validEntries = append(validEntries, entry) + } + + if err := procfile.OutputProcfile(c.procfile, c.writePath, c.delimiter, c.stdout, validEntries); err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + return 0 +} diff --git a/commands/delete_command.go b/commands/delete_command.go deleted file mode 100644 index 9e6fe95..0000000 --- a/commands/delete_command.go +++ /dev/null @@ -1,17 +0,0 @@ -package commands - -import ( - "procfile-util/procfile" -) - -func DeleteCommand(entries []procfile.ProcfileEntry, processType string, writePath string, stdout bool, delimiter string, path string) bool { - var validEntries []procfile.ProcfileEntry - for _, entry := range entries { - if processType == entry.Name { - continue - } - validEntries = append(validEntries, entry) - } - - return procfile.OutputProcfile(path, writePath, delimiter, stdout, validEntries) -} diff --git a/commands/exists.go b/commands/exists.go new file mode 100644 index 0000000..7f89ddd --- /dev/null +++ b/commands/exists.go @@ -0,0 +1,105 @@ +package commands + +import ( + "fmt" + "os" + + "github.com/josegonzalez/cli-skeleton/command" + "github.com/posener/complete" + flag "github.com/spf13/pflag" +) + +type ExistsCommand struct { + command.Meta + GlobalFlagCommand + + processType string +} + +func (c *ExistsCommand) Name() string { + return "exists" +} + +func (c *ExistsCommand) Synopsis() string { + return "Eats one or more lollipops" +} + +func (c *ExistsCommand) Help() string { + return command.CommandHelp(c) +} + +func (c *ExistsCommand) Examples() map[string]string { + appName := os.Getenv("CLI_APP_NAME") + return map[string]string{ + "Command": fmt.Sprintf("%s %s", appName, c.Name()), + } +} + +func (c *ExistsCommand) Arguments() []command.Argument { + args := []command.Argument{} + return args +} + +func (c *ExistsCommand) AutocompleteArgs() complete.Predictor { + return complete.PredictNothing +} + +func (c *ExistsCommand) ParsedArguments(args []string) (map[string]command.Argument, error) { + return command.ParseArguments(args, c.Arguments()) +} + +func (c *ExistsCommand) FlagSet() *flag.FlagSet { + f := c.Meta.FlagSet(c.Name(), command.FlagSetClient) + // required? + f.StringVarP(&c.processType, "process-type", "p", "", "name of process to delete") + c.GlobalFlags(f) + return f +} + +func (c *ExistsCommand) AutocompleteFlags() complete.Flags { + return command.MergeAutocompleteFlags( + c.Meta.AutocompleteFlags(command.FlagSetClient), + c.AutocompleteGlobalFlags(), + complete.Flags{ + "--process-type": complete.PredictAnything, + }, + ) +} + +func (c *ExistsCommand) Run(args []string) int { + flags := c.FlagSet() + flags.Usage = func() { c.Ui.Output(c.Help()) } + if err := flags.Parse(args); err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + _, err := c.ParsedArguments(flags.Args()) + if err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + entries, err := parseProcfile(c.procfile, c.delimiter, c.strict) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if len(entries) == 0 { + c.Ui.Error("No processes defined") + return 1 + } + + for _, entry := range entries { + if c.processType == entry.Name { + return 0 + } + } + + c.Ui.Error("No matching process entry found") + + return 1 +} diff --git a/commands/exists_command.go b/commands/exists_command.go deleted file mode 100644 index faba651..0000000 --- a/commands/exists_command.go +++ /dev/null @@ -1,19 +0,0 @@ -package commands - -import ( - "fmt" - "os" - - "procfile-util/procfile" -) - -func ExistsCommand(entries []procfile.ProcfileEntry, processType string) bool { - for _, entry := range entries { - if processType == entry.Name { - return true - } - } - - fmt.Fprint(os.Stderr, "no matching process entry found\n") - return false -} diff --git a/commands/expand.go b/commands/expand.go new file mode 100644 index 0000000..0ff7879 --- /dev/null +++ b/commands/expand.go @@ -0,0 +1,127 @@ +package commands + +import ( + "fmt" + "os" + "procfile-util/procfile" + + "github.com/josegonzalez/cli-skeleton/command" + "github.com/posener/complete" + flag "github.com/spf13/pflag" +) + +type ExpandCommand struct { + command.Meta + GlobalFlagCommand + + allowGetenv bool + envPath string + processType string +} + +func (c *ExpandCommand) Name() string { + return "expand" +} + +func (c *ExpandCommand) Synopsis() string { + return "Eats one or more lollipops" +} + +func (c *ExpandCommand) Help() string { + return command.CommandHelp(c) +} + +func (c *ExpandCommand) Examples() map[string]string { + appName := os.Getenv("CLI_APP_NAME") + return map[string]string{ + "Command": fmt.Sprintf("%s %s", appName, c.Name()), + } +} + +func (c *ExpandCommand) Arguments() []command.Argument { + args := []command.Argument{} + return args +} + +func (c *ExpandCommand) AutocompleteArgs() complete.Predictor { + return complete.PredictNothing +} + +func (c *ExpandCommand) ParsedArguments(args []string) (map[string]command.Argument, error) { + return command.ParseArguments(args, c.Arguments()) +} + +func (c *ExpandCommand) FlagSet() *flag.FlagSet { + f := c.Meta.FlagSet(c.Name(), command.FlagSetClient) + f.BoolVarP(&c.allowGetenv, "allow-getenv", "a", false, "allow the use of the existing env when expanding commands") + f.StringVarP(&c.envPath, "env-file", "e", "", "path to a dotenv file") + f.StringVarP(&c.processType, "process-type", "p", "", "name of process to expand") + + c.GlobalFlags(f) + return f +} + +func (c *ExpandCommand) AutocompleteFlags() complete.Flags { + return command.MergeAutocompleteFlags( + c.Meta.AutocompleteFlags(command.FlagSetClient), + c.AutocompleteGlobalFlags(), + complete.Flags{ + "--allow-getenv": complete.PredictNothing, + "--env-path": complete.PredictFiles("*"), + "--process-type": complete.PredictAnything, + }, + ) +} + +func (c *ExpandCommand) Run(args []string) int { + flags := c.FlagSet() + flags.Usage = func() { c.Ui.Output(c.Help()) } + if err := flags.Parse(args); err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + _, err := c.ParsedArguments(flags.Args()) + if err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + entries, err := parseProcfile(c.procfile, c.delimiter, c.strict) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if len(entries) == 0 { + c.Ui.Error("No processes defined") + return 1 + } + + hasErrors := false + var expandedEntries []procfile.ProcfileEntry + for _, entry := range entries { + command, err := expandEnv(entry, c.envPath, c.allowGetenv, c.defaultPort) + if err != nil { + c.Ui.Error(fmt.Sprintf("error processing command: %s", err)) + hasErrors = true + } + + entry.Command = command + expandedEntries = append(expandedEntries, entry) + } + + if hasErrors { + return 1 + } + + for _, entry := range expandedEntries { + if c.processType == "" || c.processType == entry.Name { + c.Ui.Output(fmt.Sprintf("%v%v %v", entry.Name, c.delimiter, entry.Command)) + } + } + + return 0 +} diff --git a/commands/expand_command.go b/commands/expand_command.go deleted file mode 100644 index d6706ce..0000000 --- a/commands/expand_command.go +++ /dev/null @@ -1,35 +0,0 @@ -package commands - -import ( - "fmt" - "os" - - "procfile-util/procfile" -) - -func ExpandCommand(entries []procfile.ProcfileEntry, envPath string, allowGetenv bool, processType string, defaultPort int, delimiter string) bool { - hasErrors := false - var expandedEntries []procfile.ProcfileEntry - for _, entry := range entries { - command, err := expandEnv(entry, envPath, allowGetenv, defaultPort) - if err != nil { - fmt.Fprintf(os.Stderr, "error processing command: %s\n", err) - hasErrors = true - } - - entry.Command = command - expandedEntries = append(expandedEntries, entry) - } - - if hasErrors { - return false - } - - for _, entry := range expandedEntries { - if processType == "" || processType == entry.Name { - fmt.Printf("%v%v %v\n", entry.Name, delimiter, entry.Command) - } - } - - return true -} diff --git a/commands/export.go b/commands/export.go new file mode 100644 index 0000000..e3a0e2b --- /dev/null +++ b/commands/export.go @@ -0,0 +1,299 @@ +package commands + +import ( + "fmt" + "io/ioutil" + "os" + "os/user" + "procfile-util/export" + "procfile-util/procfile" + "strconv" + "strings" + + "github.com/joho/godotenv" + "github.com/josegonzalez/cli-skeleton/command" + "github.com/posener/complete" + flag "github.com/spf13/pflag" +) + +type ExportCommand struct { + command.Meta + GlobalFlagCommand + + app string + description string + envPath string + format string + formation string + group string + home string + limitCoredump string + limitCputime string + limitData string + limitFileSize string + limitLockedMemory string + limitOpenFiles string + limitUserProcesses string + limitPhysicalMemory string + limitStackSize string + location string + logPath string + nice string + prestart string + workingDirectoryPath string + run string + timeout int + user string +} + +func (c *ExportCommand) Name() string { + return "export" +} + +func (c *ExportCommand) Synopsis() string { + return "Eats one or more lollipops" +} + +func (c *ExportCommand) Help() string { + return command.CommandHelp(c) +} + +func (c *ExportCommand) Examples() map[string]string { + appName := os.Getenv("CLI_APP_NAME") + return map[string]string{ + "Command": fmt.Sprintf("%s %s", appName, c.Name()), + } +} + +func (c *ExportCommand) Arguments() []command.Argument { + args := []command.Argument{} + return args +} + +func (c *ExportCommand) AutocompleteArgs() complete.Predictor { + return complete.PredictNothing +} + +func (c *ExportCommand) ParsedArguments(args []string) (map[string]command.Argument, error) { + return command.ParseArguments(args, c.Arguments()) +} + +func (c *ExportCommand) FlagSet() *flag.FlagSet { + f := c.Meta.FlagSet(c.Name(), command.FlagSetClient) + f.StringVar(&c.app, "app", "app", "name of app") + f.StringVar(&c.description, "description", "", "process description") + f.StringVarP(&c.envPath, "env-file", "e", "", "path to a dotenv file") + f.StringVar(&c.format, "format", "systemd", "format to export") + f.StringVar(&c.formation, "formation", "all=1", "specify what processes will run and how many") + f.StringVar(&c.group, "group", "", "group to run the command as") + f.StringVar(&c.home, "home", "", "home directory for program") + f.StringVar(&c.limitCoredump, "limit-coredump", "", "Largest size (in blocks) of a core file that can be created. (setrlimit RLIMIT_CORE)") + f.StringVar(&c.limitCputime, "limit-cputime", "", "Maximum amount of cpu time (in seconds) a program may use. (setrlimit RLIMIT_CPU)") + f.StringVar(&c.limitData, "limit-data", "", "Maximum data segment size (setrlimit RLIMIT_DATA)") + f.StringVar(&c.limitFileSize, "limit-file-size", "", "Maximum size (in blocks) of a file receiving writes (setrlimit RLIMIT_FSIZE)") + f.StringVar(&c.limitLockedMemory, "limit-locked-memory", "", "Maximum amount of memory (in bytes) lockable with mlock(2) (setrlimit RLIMIT_MEMLOCK)") + f.StringVar(&c.limitOpenFiles, "limit-open-files", "", "maximum number of open files, sockets, etc. (setrlimit RLIMIT_NOFILE)") + f.StringVar(&c.limitUserProcesses, "limit-user-processes", "", "Maximum number of running processes (or threads!) for this user id. Not recommended because this setting applies to the user, not the process group. (setrlimit RLIMIT_NPROC)") + f.StringVar(&c.limitPhysicalMemory, "limit-physical-memory", "", "Maximum resident set size (in bytes); the amount of physical memory used by a process. (setrlimit RLIMIT_RSS)") + f.StringVar(&c.limitStackSize, "limit-stack-size", "", "Maximum size (in bytes) of a stack segment (setrlimit RLIMIT_STACK)") + f.StringVar(&c.location, "location", "", "location to output to") + f.StringVar(&c.logPath, "log-path", "/var/log", "log directory") + f.StringVar(&c.nice, "nice", "", "nice level to add to this program before running") + f.StringVar(&c.prestart, "prestart", "", "A command to execute before starting and restarting. A failure of this command will cause the start/restart to abort. This is useful for health checks, config tests, or similar operations.") + f.StringVar(&c.workingDirectoryPath, "working-directory-path", "/", "working directory path for app") + f.StringVar(&c.run, "run", "", "run pid file directory, defaults to /var/run/") + f.IntVar(&c.timeout, "timeout", 5, "amount of time (in seconds) processes have to shutdown gracefully before receiving a SIGKILL") + f.StringVar(&c.user, "user", "", "user to run the command as") + c.GlobalFlags(f) + return f +} + +func (c *ExportCommand) AutocompleteFlags() complete.Flags { + return command.MergeAutocompleteFlags( + c.Meta.AutocompleteFlags(command.FlagSetClient), + c.AutocompleteGlobalFlags(), + complete.Flags{ + "--count": complete.PredictAnything, + "--app": complete.PredictAnything, + "--description": complete.PredictAnything, + "--env-file": complete.PredictFiles("*"), + "--format": complete.PredictAnything, + "--formation": complete.PredictAnything, + "--group": complete.PredictAnything, + "--home": complete.PredictAnything, + "--limit-coredump": complete.PredictAnything, + "--limit-cputime": complete.PredictAnything, + "--limit-data": complete.PredictAnything, + "--limit-file-size": complete.PredictAnything, + "--limit-locked-memory": complete.PredictAnything, + "--limit-open-files": complete.PredictAnything, + "--limit-user-processes": complete.PredictAnything, + "--limit-physical-memory": complete.PredictAnything, + "--limit-stack-size": complete.PredictAnything, + "--location": complete.PredictAnything, + "--log-path": complete.PredictAnything, + "--nice": complete.PredictAnything, + "--prestart": complete.PredictAnything, + "--working-directory-path": complete.PredictAnything, + "--run": complete.PredictAnything, + "--timeout": complete.PredictAnything, + "--user": complete.PredictAnything, + }, + ) +} + +func (c *ExportCommand) Run(args []string) int { + flags := c.FlagSet() + flags.Usage = func() { c.Ui.Output(c.Help()) } + if err := flags.Parse(args); err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + _, err := c.ParsedArguments(flags.Args()) + if err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + entries, err := parseProcfile(c.procfile, c.delimiter, c.strict) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if len(entries) == 0 { + c.Ui.Error("No processes defined") + return 1 + } + + if c.format == "" { + fmt.Fprintf(os.Stderr, "no format specified\n") + return 1 + } + if c.location == "" { + fmt.Fprintf(os.Stderr, "no output location specified\n") + return 1 + } + + formats := map[string]export.ExportFunc{ + "launchd": export.ExportLaunchd, + "runit": export.ExportRunit, + "systemd": export.ExportSystemd, + "systemd-user": export.ExportSystemdUser, + "sysv": export.ExportSysv, + "upstart": export.ExportUpstart, + } + + if _, ok := formats[c.format]; !ok { + c.Ui.Error(fmt.Sprintf("Invalid format type: %s", c.format)) + return 1 + } + + formations, err := procfile.ParseFormation(c.formation) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if c.user == "" { + c.user = c.app + } + + if c.group == "" { + c.group = c.app + } + + u, err := user.Current() + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if c.home == "" { + c.home = "/home/" + u.Username + } + + env := make(map[string]string) + if c.envPath != "" { + b, err := ioutil.ReadFile(c.envPath) + if err != nil { + fmt.Fprintf(os.Stderr, "error reading env file: %s\n", err) + return 1 + } + + content := string(b) + env, err = godotenv.Unmarshal(content) + if err != nil { + fmt.Fprintf(os.Stderr, "error parsing env file: %s\n", err) + return 1 + } + } + + vars := make(map[string]interface{}) + vars["app"] = c.app + vars["description"] = c.description + vars["env"] = env + vars["group"] = c.group + vars["home"] = c.home + vars["log"] = c.logPath + vars["location"] = c.location + vars["limit_coredump"] = c.limitCoredump + vars["limit_cputime"] = c.limitCputime + vars["limit_data"] = c.limitData + vars["limit_file_size"] = c.limitFileSize + vars["limit_locked_memory"] = c.limitLockedMemory + vars["limit_open_files"] = c.limitOpenFiles + vars["limit_user_processes"] = c.limitUserProcesses + vars["limit_physical_memory"] = c.limitPhysicalMemory + vars["limit_stack_size"] = c.limitStackSize + vars["nice"] = c.nice + vars["prestart"] = c.prestart + vars["working_directory"] = c.workingDirectoryPath + vars["timeout"] = strconv.Itoa(c.timeout) + vars["ulimit_shell"] = ulimitShell(c.limitCoredump, c.limitCputime, c.limitData, c.limitFileSize, c.limitLockedMemory, c.limitOpenFiles, c.limitUserProcesses, c.limitPhysicalMemory, c.limitStackSize) + vars["user"] = c.user + + if fn, ok := formats[c.format]; ok { + if !fn(c.app, entries, formations, c.location, c.defaultPort, vars, c.Ui) { + return 1 + } + } + + return 0 +} + +func ulimitShell(limitCoredump string, limitCputime string, limitData string, limitFileSize string, limitLockedMemory string, limitOpenFiles string, limitUserProcesses string, limitPhysicalMemory string, limitStackSize string) string { + s := []string{} + if limitCoredump != "" { + s = append(s, "ulimit -c ${limit_coredump}") + } + if limitCputime != "" { + s = append(s, "ulimit -t ${limit_cputime}") + } + if limitData != "" { + s = append(s, "ulimit -d ${limit_data}") + } + if limitFileSize != "" { + s = append(s, "ulimit -f ${limit_file_size}") + } + if limitLockedMemory != "" { + s = append(s, "ulimit -l ${limit_locked_memory}") + } + if limitOpenFiles != "" { + s = append(s, "ulimit -n ${limit_open_files}") + } + if limitUserProcesses != "" { + s = append(s, "ulimit -u ${limit_user_processes}") + } + if limitPhysicalMemory != "" { + s = append(s, "ulimit -m ${limit_physical_memory}") + } + if limitStackSize != "" { + s = append(s, "ulimit -s ${limit_stack_size}") + } + + return strings.Join(s, "\n") +} diff --git a/commands/export_command.go b/commands/export_command.go deleted file mode 100644 index 2e114b0..0000000 --- a/commands/export_command.go +++ /dev/null @@ -1,143 +0,0 @@ -package commands - -import ( - "fmt" - "io/ioutil" - "os" - "os/user" - "strconv" - "strings" - - "procfile-util/export" - "procfile-util/procfile" - - "github.com/joho/godotenv" -) - -func ExportCommand(entries []procfile.ProcfileEntry, app string, description string, envPath string, format string, formation string, group string, home string, limitCoredump string, limitCputime string, limitData string, limitFileSize string, limitLockedMemory string, limitOpenFiles string, limitUserProcesses string, limitPhysicalMemory string, limitStackSize string, location string, logPath string, nice string, prestart string, workingDirectoryPath string, runPath string, timeout int, processUser string, defaultPort int) bool { - if format == "" { - fmt.Fprintf(os.Stderr, "no format specified\n") - return false - } - if location == "" { - fmt.Fprintf(os.Stderr, "no output location specified\n") - return false - } - - formats := map[string]export.ExportFunc{ - "launchd": export.ExportLaunchd, - "runit": export.ExportRunit, - "systemd": export.ExportSystemd, - "systemd-user": export.ExportSystemdUser, - "sysv": export.ExportSysv, - "upstart": export.ExportUpstart, - } - - if _, ok := formats[format]; !ok { - fmt.Fprintf(os.Stderr, "invalid format type: %s\n", format) - return false - } - - formations, err := procfile.ParseFormation(formation) - if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) - return false - } - - if processUser == "" { - processUser = app - } - - if group == "" { - group = app - } - - u, err := user.Current() - if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) - return false - } - - if home == "" { - home = "/home/" + u.Username - } - - env := make(map[string]string) - if envPath != "" { - b, err := ioutil.ReadFile(envPath) - if err != nil { - fmt.Fprintf(os.Stderr, "error reading env file: %s\n", err) - return false - } - - content := string(b) - env, err = godotenv.Unmarshal(content) - if err != nil { - fmt.Fprintf(os.Stderr, "error parsing env file: %s\n", err) - return false - } - } - - vars := make(map[string]interface{}) - vars["app"] = app - vars["description"] = description - vars["env"] = env - vars["group"] = group - vars["home"] = home - vars["log"] = logPath - vars["location"] = location - vars["limit_coredump"] = limitCoredump - vars["limit_cputime"] = limitCputime - vars["limit_data"] = limitData - vars["limit_file_size"] = limitFileSize - vars["limit_locked_memory"] = limitLockedMemory - vars["limit_open_files"] = limitOpenFiles - vars["limit_user_processes"] = limitUserProcesses - vars["limit_physical_memory"] = limitPhysicalMemory - vars["limit_stack_size"] = limitStackSize - vars["nice"] = nice - vars["prestart"] = prestart - vars["working_directory"] = workingDirectoryPath - vars["timeout"] = strconv.Itoa(timeout) - vars["ulimit_shell"] = ulimitShell(limitCoredump, limitCputime, limitData, limitFileSize, limitLockedMemory, limitOpenFiles, limitUserProcesses, limitPhysicalMemory, limitStackSize) - vars["user"] = processUser - - if fn, ok := formats[format]; ok { - return fn(app, entries, formations, location, defaultPort, vars) - } - - return false -} - -func ulimitShell(limitCoredump string, limitCputime string, limitData string, limitFileSize string, limitLockedMemory string, limitOpenFiles string, limitUserProcesses string, limitPhysicalMemory string, limitStackSize string) string { - s := []string{} - if limitCoredump != "" { - s = append(s, "ulimit -c ${limit_coredump}") - } - if limitCputime != "" { - s = append(s, "ulimit -t ${limit_cputime}") - } - if limitData != "" { - s = append(s, "ulimit -d ${limit_data}") - } - if limitFileSize != "" { - s = append(s, "ulimit -f ${limit_file_size}") - } - if limitLockedMemory != "" { - s = append(s, "ulimit -l ${limit_locked_memory}") - } - if limitOpenFiles != "" { - s = append(s, "ulimit -n ${limit_open_files}") - } - if limitUserProcesses != "" { - s = append(s, "ulimit -u ${limit_user_processes}") - } - if limitPhysicalMemory != "" { - s = append(s, "ulimit -m ${limit_physical_memory}") - } - if limitStackSize != "" { - s = append(s, "ulimit -s ${limit_stack_size}") - } - - return strings.Join(s, "\n") -} diff --git a/commands/flags.go b/commands/flags.go new file mode 100644 index 0000000..99b013d --- /dev/null +++ b/commands/flags.go @@ -0,0 +1,29 @@ +package commands + +import ( + "github.com/posener/complete" + flag "github.com/spf13/pflag" +) + +type GlobalFlagCommand struct { + procfile string + delimiter string + defaultPort int + strict bool +} + +func (c *GlobalFlagCommand) GlobalFlags(f *flag.FlagSet) { + f.StringVarP(&c.procfile, "procfile", "P", "Procfile", "path to a procfile") + f.StringVarP(&c.delimiter, "delimiter", "D", ":", "delimiter in use within procfile") + f.IntVarP(&c.defaultPort, "default-port", "d", 5000, "default port to use") + f.BoolVarP(&c.strict, "strict", "S", false, "strictly parse the Procfile") +} + +func (c *GlobalFlagCommand) AutocompleteGlobalFlags() complete.Flags { + return complete.Flags{ + "--procfile": complete.PredictAnything, + "--delimiter": complete.PredictAnything, + "--default-port": complete.PredictAnything, + "--strict": complete.PredictNothing, + } +} diff --git a/commands/list.go b/commands/list.go new file mode 100644 index 0000000..940c48f --- /dev/null +++ b/commands/list.go @@ -0,0 +1,95 @@ +package commands + +import ( + "fmt" + "os" + + "github.com/josegonzalez/cli-skeleton/command" + "github.com/posener/complete" + flag "github.com/spf13/pflag" +) + +type ListCommand struct { + command.Meta + GlobalFlagCommand +} + +func (c *ListCommand) Name() string { + return "list" +} + +func (c *ListCommand) Synopsis() string { + return "Eats one or more lollipops" +} + +func (c *ListCommand) Help() string { + return command.CommandHelp(c) +} + +func (c *ListCommand) Examples() map[string]string { + appName := os.Getenv("CLI_APP_NAME") + return map[string]string{ + "Command": fmt.Sprintf("%s %s", appName, c.Name()), + } +} + +func (c *ListCommand) Arguments() []command.Argument { + args := []command.Argument{} + return args +} + +func (c *ListCommand) AutocompleteArgs() complete.Predictor { + return complete.PredictNothing +} + +func (c *ListCommand) ParsedArguments(args []string) (map[string]command.Argument, error) { + return command.ParseArguments(args, c.Arguments()) +} + +func (c *ListCommand) FlagSet() *flag.FlagSet { + f := c.Meta.FlagSet(c.Name(), command.FlagSetClient) + c.GlobalFlags(f) + return f +} + +func (c *ListCommand) AutocompleteFlags() complete.Flags { + return command.MergeAutocompleteFlags( + c.Meta.AutocompleteFlags(command.FlagSetClient), + c.AutocompleteGlobalFlags(), + complete.Flags{}, + ) +} + +func (c *ListCommand) Run(args []string) int { + flags := c.FlagSet() + flags.Usage = func() { c.Ui.Output(c.Help()) } + if err := flags.Parse(args); err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + _, err := c.ParsedArguments(flags.Args()) + if err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + entries, err := parseProcfile(c.procfile, c.delimiter, c.strict) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if len(entries) == 0 { + c.Ui.Error("No processes defined") + return 1 + } + + for _, entry := range entries { + c.Ui.Output(entry.Name) + } + + return 0 +} diff --git a/commands/list_command.go b/commands/list_command.go deleted file mode 100644 index f2fba53..0000000 --- a/commands/list_command.go +++ /dev/null @@ -1,14 +0,0 @@ -package commands - -import ( - "fmt" - - "procfile-util/procfile" -) - -func ListCommand(entries []procfile.ProcfileEntry) bool { - for _, entry := range entries { - fmt.Printf("%v\n", entry.Name) - } - return true -} diff --git a/commands/commands.go b/commands/main.go similarity index 81% rename from commands/commands.go rename to commands/main.go index 033fcf1..bab3ecc 100644 --- a/commands/commands.go +++ b/commands/main.go @@ -3,16 +3,25 @@ package commands import ( "io/ioutil" "os" + "procfile-util/procfile" "strconv" "strings" - "procfile-util/procfile" - "github.com/joho/godotenv" ) const portEnvVar = "PORT" +func parseProcfile(path string, delimiter string, strict bool) ([]procfile.ProcfileEntry, error) { + var entries []procfile.ProcfileEntry + text, err := procfile.GetProcfileContent(path) + if err != nil { + return entries, err + } + + return procfile.ParseProcfile(text, delimiter, strict) +} + func expandEnv(e procfile.ProcfileEntry, envPath string, allowEnv bool, defaultPort int) (string, error) { baseExpandFunc := func(key string) string { if key == "PS" { diff --git a/commands/set.go b/commands/set.go new file mode 100644 index 0000000..a9cb2eb --- /dev/null +++ b/commands/set.go @@ -0,0 +1,126 @@ +package commands + +import ( + "fmt" + "os" + "procfile-util/procfile" + + "github.com/josegonzalez/cli-skeleton/command" + "github.com/posener/complete" + flag "github.com/spf13/pflag" +) + +type SetCommand struct { + command.Meta + GlobalFlagCommand + + processType string + command string + stdout bool + writePath string +} + +func (c *SetCommand) Name() string { + return "set" +} + +func (c *SetCommand) Synopsis() string { + return "Eats one or more lollipops" +} + +func (c *SetCommand) Help() string { + return command.CommandHelp(c) +} + +func (c *SetCommand) Examples() map[string]string { + appName := os.Getenv("CLI_APP_NAME") + return map[string]string{ + "Command": fmt.Sprintf("%s %s", appName, c.Name()), + } +} + +func (c *SetCommand) Arguments() []command.Argument { + args := []command.Argument{} + return args +} + +func (c *SetCommand) AutocompleteArgs() complete.Predictor { + return complete.PredictNothing +} + +func (c *SetCommand) ParsedArguments(args []string) (map[string]command.Argument, error) { + return command.ParseArguments(args, c.Arguments()) +} + +func (c *SetCommand) FlagSet() *flag.FlagSet { + f := c.Meta.FlagSet(c.Name(), command.FlagSetClient) + // Required + f.StringVarP(&c.processType, "process-type", "p", "", "name of process to set") + // Required + f.StringVarP(&c.command, "command", "c", "", "command to set") + f.BoolVarP(&c.stdout, "stdout", "s", false, "write output to stdout") + f.StringVarP(&c.writePath, "write-path", "w", "", "path to Procfile to write to") + + c.GlobalFlags(f) + return f +} + +func (c *SetCommand) AutocompleteFlags() complete.Flags { + return command.MergeAutocompleteFlags( + c.Meta.AutocompleteFlags(command.FlagSetClient), + c.AutocompleteGlobalFlags(), + complete.Flags{ + "--process-type": complete.PredictAnything, + "--command": complete.PredictAnything, + "--sdout": complete.PredictNothing, + "--write-path": complete.PredictAnything, + }, + ) +} + +func (c *SetCommand) Run(args []string) int { + flags := c.FlagSet() + flags.Usage = func() { c.Ui.Output(c.Help()) } + if err := flags.Parse(args); err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + _, err := c.ParsedArguments(flags.Args()) + if err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + entries, err := parseProcfile(c.procfile, c.delimiter, c.strict) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if len(entries) == 0 { + c.Ui.Error("No processes defined") + return 1 + } + + var validEntries []procfile.ProcfileEntry + validEntries = append(validEntries, procfile.ProcfileEntry{ + Name: c.processType, + Command: c.command, + }) + for _, entry := range entries { + if c.processType == entry.Name { + continue + } + validEntries = append(validEntries, entry) + } + + if err := procfile.OutputProcfile(c.procfile, c.writePath, c.delimiter, c.stdout, validEntries); err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + return 0 +} diff --git a/commands/set_command.go b/commands/set_command.go deleted file mode 100644 index 9e3f694..0000000 --- a/commands/set_command.go +++ /dev/null @@ -1,18 +0,0 @@ -package commands - -import ( - "procfile-util/procfile" -) - -func SetCommand(entries []procfile.ProcfileEntry, processType string, command string, writePath string, stdout bool, delimiter string, path string) bool { - var validEntries []procfile.ProcfileEntry - validEntries = append(validEntries, procfile.ProcfileEntry{processType, command}) - for _, entry := range entries { - if processType == entry.Name { - continue - } - validEntries = append(validEntries, entry) - } - - return procfile.OutputProcfile(path, writePath, delimiter, stdout, validEntries) -} \ No newline at end of file diff --git a/commands/show.go b/commands/show.go new file mode 100644 index 0000000..89ccbd6 --- /dev/null +++ b/commands/show.go @@ -0,0 +1,126 @@ +package commands + +import ( + "fmt" + "os" + "procfile-util/procfile" + + "github.com/josegonzalez/cli-skeleton/command" + "github.com/posener/complete" + flag "github.com/spf13/pflag" +) + +type ShowCommand struct { + command.Meta + GlobalFlagCommand + + allowGetenv bool + envPath string + processType string +} + +func (c *ShowCommand) Name() string { + return "show" +} + +func (c *ShowCommand) Synopsis() string { + return "Eats one or more lollipops" +} + +func (c *ShowCommand) Help() string { + return command.CommandHelp(c) +} + +func (c *ShowCommand) Examples() map[string]string { + appName := os.Getenv("CLI_APP_NAME") + return map[string]string{ + "Command": fmt.Sprintf("%s %s", appName, c.Name()), + } +} + +func (c *ShowCommand) Arguments() []command.Argument { + args := []command.Argument{} + return args +} + +func (c *ShowCommand) AutocompleteArgs() complete.Predictor { + return complete.PredictNothing +} + +func (c *ShowCommand) ParsedArguments(args []string) (map[string]command.Argument, error) { + return command.ParseArguments(args, c.Arguments()) +} + +func (c *ShowCommand) FlagSet() *flag.FlagSet { + f := c.Meta.FlagSet(c.Name(), command.FlagSetClient) + f.BoolVarP(&c.allowGetenv, "allow-getenv", "a", false, "allow the use of the existing env when expanding commands") + f.StringVarP(&c.envPath, "env-file", "e", "", "path to a dotenv file") + // required? + f.StringVarP(&c.processType, "process-type", "p", "", "name of process to show") + + c.GlobalFlags(f) + return f +} + +func (c *ShowCommand) AutocompleteFlags() complete.Flags { + return command.MergeAutocompleteFlags( + c.Meta.AutocompleteFlags(command.FlagSetClient), + c.AutocompleteGlobalFlags(), + complete.Flags{ + "--allow-getenv": complete.PredictNothing, + "--env-file": complete.PredictFiles("*"), + "--process-type": complete.PredictAnything, + }, + ) +} + +func (c *ShowCommand) Run(args []string) int { + flags := c.FlagSet() + flags.Usage = func() { c.Ui.Output(c.Help()) } + if err := flags.Parse(args); err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + _, err := c.ParsedArguments(flags.Args()) + if err != nil { + c.Ui.Error(err.Error()) + c.Ui.Error(command.CommandErrorText(c)) + return 1 + } + + entries, err := parseProcfile(c.procfile, c.delimiter, c.strict) + if err != nil { + c.Ui.Error(err.Error()) + return 1 + } + + if len(entries) == 0 { + c.Ui.Error("No processes defined") + return 1 + } + + var foundEntry procfile.ProcfileEntry + for _, entry := range entries { + if c.processType == entry.Name { + foundEntry = entry + break + } + } + + if foundEntry == (procfile.ProcfileEntry{}) { + c.Ui.Error("No matching process entry found") + return 1 + } + + command, err := expandEnv(foundEntry, c.envPath, c.allowGetenv, c.defaultPort) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error processing command: %s", err)) + return 1 + } + + c.Ui.Output(command) + + return 0 +} diff --git a/commands/show_command.go b/commands/show_command.go deleted file mode 100644 index 53a59ae..0000000 --- a/commands/show_command.go +++ /dev/null @@ -1,32 +0,0 @@ -package commands - -import ( - "fmt" - "os" - - "procfile-util/procfile" -) - -func ShowCommand(entries []procfile.ProcfileEntry, envPath string, allowGetenv bool, processType string, defaultPort int) bool { - var foundEntry procfile.ProcfileEntry - for _, entry := range entries { - if processType == entry.Name { - foundEntry = entry - break - } - } - - if foundEntry == (procfile.ProcfileEntry{}) { - fmt.Fprintf(os.Stderr, "no matching process entry found\n") - return false - } - - command, err := expandEnv(foundEntry, envPath, allowGetenv, defaultPort) - if err != nil { - fmt.Fprintf(os.Stderr, "error processing command: %s\n", err) - return false - } - - fmt.Printf("%v\n", command) - return true -} diff --git a/export/export.go b/export/export.go index b64afac..ad6c59f 100644 --- a/export/export.go +++ b/export/export.go @@ -7,9 +7,11 @@ import ( "text/template" "procfile-util/procfile" + + "github.com/mitchellh/cli" ) -type ExportFunc func(string, []procfile.ProcfileEntry, map[string]procfile.FormationEntry, string, int, map[string]interface{}) bool +type ExportFunc func(string, []procfile.ProcfileEntry, map[string]procfile.FormationEntry, string, int, map[string]interface{}, cli.Ui) bool func processCount(entry procfile.ProcfileEntry, formations map[string]procfile.FormationEntry) int { count := 0 @@ -45,26 +47,23 @@ func templateVars(app string, entry procfile.ProcfileEntry, processName string, return config } -func writeOutput(t *template.Template, outputPath string, variables map[string]interface{}) bool { +func writeOutput(t *template.Template, outputPath string, variables map[string]interface{}) error { fmt.Println("writing:", outputPath) f, err := os.Create(outputPath) if err != nil { - fmt.Fprintf(os.Stderr, "error creating file: %s\n", err) - return false + return fmt.Errorf("error creating file: %w", err) } defer f.Close() if err = t.Execute(f, variables); err != nil { - fmt.Fprintf(os.Stderr, "error writing output: %s\n", err) - return false + return fmt.Errorf("error writing output: %w", err) } if err := os.Chmod(outputPath, 0755); err != nil { - fmt.Fprintf(os.Stderr, "error setting mode: %s\n", err) - return false + return fmt.Errorf("error setting mode: %w", err) } - return true + return nil } func loadTemplate(name string, filename string) (*template.Template, error) { diff --git a/export/export_launchd.go b/export/export_launchd.go index 80ce106..be3f6d8 100644 --- a/export/export_launchd.go +++ b/export/export_launchd.go @@ -5,12 +5,14 @@ import ( "os" "procfile-util/procfile" + + "github.com/mitchellh/cli" ) -func ExportLaunchd(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool { +func ExportLaunchd(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}, ui cli.Ui) bool { l, err := loadTemplate("launchd", "templates/launchd/launchd.plist.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } @@ -26,7 +28,8 @@ func ExportLaunchd(app string, entries []procfile.ProcfileEntry, formations map[ processName := fmt.Sprintf("%s-%d", entry.Name, num) port := portFor(i, num, defaultPort) config := templateVars(app, entry, processName, num, port, vars) - if !writeOutput(l, fmt.Sprintf("%s/Library/LaunchDaemons/%s-%s.plist", location, app, processName), config) { + if err := writeOutput(l, fmt.Sprintf("%s/Library/LaunchDaemons/%s-%s.plist", location, app, processName), config); err != nil { + ui.Error(err.Error()) return false } diff --git a/export/export_runit.go b/export/export_runit.go index 445e4ba..6590eb1 100644 --- a/export/export_runit.go +++ b/export/export_runit.go @@ -6,17 +6,19 @@ import ( "strconv" "procfile-util/procfile" + + "github.com/mitchellh/cli" ) -func ExportRunit(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool { +func ExportRunit(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}, ui cli.Ui) bool { r, err := loadTemplate("run", "templates/runit/run.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } l, err := loadTemplate("log", "templates/runit/log/run.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } @@ -33,25 +35,26 @@ func ExportRunit(app string, entries []procfile.ProcfileEntry, formations map[st folderPath := location + "/service/" + processDirectory processName := fmt.Sprintf("%s-%d", entry.Name, num) - fmt.Println("creating:", folderPath) + ui.Output(fmt.Sprintf("creating: %s", folderPath)) os.MkdirAll(folderPath, os.ModePerm) - fmt.Println("creating:", folderPath+"/env") + ui.Output(fmt.Sprintf("creating: %s/env", folderPath)) os.MkdirAll(folderPath+"/env", os.ModePerm) - fmt.Println("creating:", folderPath+"/log") + ui.Output(fmt.Sprintf("creating: %s/log", folderPath)) os.MkdirAll(folderPath+"/log", os.ModePerm) port := portFor(i, num, defaultPort) config := templateVars(app, entry, processName, num, port, vars) - if !writeOutput(r, fmt.Sprintf("%s/run", folderPath), config) { + if err := writeOutput(r, fmt.Sprintf("%s/run", folderPath), config); err != nil { + ui.Error(err.Error()) return false } env, ok := config["env"].(map[string]string) if !ok { - fmt.Fprintf(os.Stderr, "invalid env map\n") + ui.Error("Invalid env map") return false } @@ -59,26 +62,27 @@ func ExportRunit(app string, entries []procfile.ProcfileEntry, formations map[st env["PS"] = app + "-" + processName for key, value := range env { - fmt.Println("writing:", folderPath+"/env/"+key) + ui.Output(fmt.Sprintf("writing: %s/env/%s", folderPath, key)) f, err := os.Create(folderPath + "/env/" + key) if err != nil { - fmt.Fprintf(os.Stderr, "error creating file: %s\n", err) + ui.Error(fmt.Sprintf("Error creating file: %s", err)) return false } defer f.Close() if _, err = f.WriteString(value); err != nil { - fmt.Fprintf(os.Stderr, "error writing output: %s\n", err) + ui.Error(fmt.Sprintf("Error writing output: %s", err)) return false } if err = f.Sync(); err != nil { - fmt.Fprintf(os.Stderr, "error syncing output: %s\n", err) + ui.Error(fmt.Sprintf("Error syncing output: %s", err)) return false } } - if !writeOutput(l, fmt.Sprintf("%s/log/run", folderPath), config) { + if err := writeOutput(l, fmt.Sprintf("%s/log/run", folderPath), config); err != nil { + ui.Error(err.Error()) return false } diff --git a/export/export_systemd.go b/export/export_systemd.go index 55231c2..e9cd282 100644 --- a/export/export_systemd.go +++ b/export/export_systemd.go @@ -5,18 +5,20 @@ import ( "os" "procfile-util/procfile" + + "github.com/mitchellh/cli" ) -func ExportSystemd(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool { +func ExportSystemd(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}, ui cli.Ui) bool { t, err := loadTemplate("target", "templates/systemd/default/control.target.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } s, err := loadTemplate("service", "templates/systemd/default/program.service.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } @@ -36,7 +38,8 @@ func ExportSystemd(app string, entries []procfile.ProcfileEntry, formations map[ port := portFor(i, num, defaultPort) config := templateVars(app, entry, processName, num, port, vars) - if !writeOutput(s, fmt.Sprintf("%s/etc/systemd/system/%s-%s.service", location, app, fileName), config) { + if err := writeOutput(s, fmt.Sprintf("%s/etc/systemd/system/%s-%s.service", location, app, fileName), config); err != nil { + ui.Error(err.Error()) return false } @@ -46,8 +49,9 @@ func ExportSystemd(app string, entries []procfile.ProcfileEntry, formations map[ config := vars config["processes"] = processes - if writeOutput(t, fmt.Sprintf("%s/etc/systemd/system/%s.target", location, app), config) { - fmt.Println("You will want to run 'systemctl --system daemon-reload' to activate the service on the target host") + if err := writeOutput(t, fmt.Sprintf("%s/etc/systemd/system/%s.target", location, app), config); err != nil { + ui.Error(err.Error()) + ui.Output("You will want to run 'systemctl --system daemon-reload' to activate the service on the target host") return true } diff --git a/export/export_systemd_user.go b/export/export_systemd_user.go index 48c1458..98db36c 100644 --- a/export/export_systemd_user.go +++ b/export/export_systemd_user.go @@ -5,12 +5,14 @@ import ( "os" "procfile-util/procfile" + + "github.com/mitchellh/cli" ) -func ExportSystemdUser(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool { +func ExportSystemdUser(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}, ui cli.Ui) bool { s, err := loadTemplate("service", "templates/systemd-user/default/program.service.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } @@ -27,7 +29,8 @@ func ExportSystemdUser(app string, entries []procfile.ProcfileEntry, formations processName := fmt.Sprintf("%s-%d", entry.Name, num) port := portFor(i, num, defaultPort) config := templateVars(app, entry, processName, num, port, vars) - if !writeOutput(s, fmt.Sprintf("%s%s%s-%s.service", location, path, app, processName), config) { + if err := writeOutput(s, fmt.Sprintf("%s%s%s-%s.service", location, path, app, processName), config); err != nil { + ui.Error(err.Error()) return false } @@ -35,6 +38,6 @@ func ExportSystemdUser(app string, entries []procfile.ProcfileEntry, formations } } - fmt.Println("You will want to run 'systemctl --user daemon-reload' to activate the service on the target host") + ui.Output("You will want to run 'systemctl --user daemon-reload' to activate the service on the target host") return true } diff --git a/export/export_sysv.go b/export/export_sysv.go index bfc0905..6becbc2 100644 --- a/export/export_sysv.go +++ b/export/export_sysv.go @@ -5,12 +5,14 @@ import ( "os" "procfile-util/procfile" + + "github.com/mitchellh/cli" ) -func ExportSysv(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool { +func ExportSysv(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}, ui cli.Ui) bool { l, err := loadTemplate("launchd", "templates/sysv/default/init.sh.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } @@ -26,7 +28,8 @@ func ExportSysv(app string, entries []procfile.ProcfileEntry, formations map[str processName := fmt.Sprintf("%s-%d", entry.Name, num) port := portFor(i, num, defaultPort) config := templateVars(app, entry, processName, num, port, vars) - if !writeOutput(l, fmt.Sprintf("%s/etc/init.d/%s-%s", location, app, processName), config) { + if err := writeOutput(l, fmt.Sprintf("%s/etc/init.d/%s-%s", location, app, processName), config); err != nil { + ui.Error(err.Error()) return false } diff --git a/export/export_upstart.go b/export/export_upstart.go index b044210..2275c74 100644 --- a/export/export_upstart.go +++ b/export/export_upstart.go @@ -5,24 +5,26 @@ import ( "os" "procfile-util/procfile" + + "github.com/mitchellh/cli" ) -func ExportUpstart(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}) bool { +func ExportUpstart(app string, entries []procfile.ProcfileEntry, formations map[string]procfile.FormationEntry, location string, defaultPort int, vars map[string]interface{}, ui cli.Ui) bool { p, err := loadTemplate("program", "templates/upstart/default/program.conf.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } c, err := loadTemplate("app", "templates/upstart/default/control.conf.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } t, err := loadTemplate("process-type", "templates/upstart/default/process-type.conf.tmpl") if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + ui.Error(err.Error()) return false } @@ -37,7 +39,8 @@ func ExportUpstart(app string, entries []procfile.ProcfileEntry, formations map[ if count > 0 { config := vars config["process_type"] = entry.Name - if !writeOutput(t, fmt.Sprintf("%s/etc/init/%s-%s.conf", location, app, entry.Name), config) { + if err := writeOutput(t, fmt.Sprintf("%s/etc/init/%s-%s.conf", location, app, entry.Name), config); err != nil { + ui.Error(err.Error()) return false } } @@ -47,7 +50,8 @@ func ExportUpstart(app string, entries []procfile.ProcfileEntry, formations map[ fileName := fmt.Sprintf("%s-%d", entry.Name, num) port := portFor(i, num, defaultPort) config := templateVars(app, entry, processName, num, port, vars) - if !writeOutput(p, fmt.Sprintf("%s/etc/init/%s-%s.conf", location, app, fileName), config) { + if err := writeOutput(p, fmt.Sprintf("%s/etc/init/%s-%s.conf", location, app, fileName), config); err != nil { + ui.Error(err.Error()) return false } @@ -56,5 +60,10 @@ func ExportUpstart(app string, entries []procfile.ProcfileEntry, formations map[ } config := vars - return writeOutput(c, fmt.Sprintf("%s/etc/init/%s.conf", location, app), config) + if err := writeOutput(c, fmt.Sprintf("%s/etc/init/%s.conf", location, app), config); err != nil { + ui.Error(err.Error()) + return false + } + + return true } diff --git a/fixtures/forwardslash-comments.Procfile b/fixtures/forwardslash-comments.Procfile new file mode 100644 index 0000000..29db747 --- /dev/null +++ b/fixtures/forwardslash-comments.Procfile @@ -0,0 +1,5 @@ +// ignore this +web: python web.py +# this too +worker: node worker.js # testing inline comment +worker-2: node worker.js // testing inline forwardslash comment \ No newline at end of file diff --git a/go.mod b/go.mod index 386a1ee..fc5a1e7 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,37 @@ module procfile-util go 1.19 require ( - github.com/akamensky/argparse v1.4.0 github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2 github.com/joho/godotenv v1.5.1 + github.com/josegonzalez/cli-skeleton v0.15.0 + github.com/mitchellh/cli v1.1.5 + github.com/posener/complete v1.2.3 + github.com/spf13/pflag v1.0.5 gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61 ) -require github.com/alessio/shellescape v1.4.1 // indirect +require ( + github.com/Masterminds/goutils v1.1.1 // indirect + github.com/Masterminds/semver/v3 v3.1.1 // indirect + github.com/Masterminds/sprig/v3 v3.2.2 // indirect + github.com/alessio/shellescape v1.4.1 // indirect + github.com/armon/go-radix v1.0.0 // indirect + github.com/bgentry/speakeasy v0.1.0 // indirect + github.com/fatih/color v1.13.0 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/hashicorp/errwrap v1.1.0 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect + github.com/huandu/xstrings v1.3.2 // indirect + github.com/imdario/mergo v0.3.13 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect + github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/mitchellh/reflectwalk v1.0.2 // indirect + github.com/rs/zerolog v1.32.0 // indirect + github.com/shopspring/decimal v1.3.1 // indirect + github.com/spf13/cast v1.5.0 // indirect + golang.org/x/crypto v0.18.0 // indirect + golang.org/x/sys v0.16.0 // indirect + golang.org/x/term v0.16.0 // indirect +) diff --git a/go.sum b/go.sum index 757316a..dd75e69 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,119 @@ -github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc= -github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA= +github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= +github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= +github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= +github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= +github.com/Masterminds/sprig/v3 v3.2.1/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk= +github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8= +github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk= github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0= github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30= github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2 h1:axBiC50cNZOs7ygH5BgQp4N+aYrZ2DNpWZ1KG3VOSOM= github.com/andrew-d/go-termutil v0.0.0-20150726205930-009166a695a2/go.mod h1:jnzFpU88PccN/tPPhCpnNU8mZphvKxYM9lLNkd8e+os= +github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= +github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY= +github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= +github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw= +github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= +github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= +github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/josegonzalez/cli-skeleton v0.15.0 h1:8AuxPC+KioDnBf9K+ZIE+1tYbayOUBJAluoUnCyHdIc= +github.com/josegonzalez/cli-skeleton v0.15.0/go.mod h1:iCpaNFH5JS8kk8VfEsa+Ml6VNw/3oIIPYV7XDXaBypA= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mitchellh/cli v1.1.5 h1:OxRIeJXpAMztws/XHlN2vu6imG5Dpq+j61AzAX5fLng= +github.com/mitchellh/cli v1.1.5/go.mod h1:v8+iFts2sPIKUV1ltktPXMCC8fumSKFItNcD2cLtRR4= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= +github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= +github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= +github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo= +github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s= +github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= +github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= +github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= +github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= +github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= +github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= +golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61 h1:8ajkpB4hXVftY5ko905id+dOnmorcS2CHNxxHLLDcFM= gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61/go.mod h1:IfMagxm39Ys4ybJrDb7W3Ob8RwxftP0Yy+or/NVz1O8= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index 2825494..bdb1888 100644 --- a/main.go +++ b/main.go @@ -1,143 +1,72 @@ package main import ( + "context" "fmt" "os" - "strconv" - "procfile-util/procfile" "procfile-util/commands" - "github.com/akamensky/argparse" + "github.com/josegonzalez/cli-skeleton/command" + "github.com/mitchellh/cli" ) -// Version contains the procfile-util version -var Version string - -func parseProcfile(path string, delimiter string, strict bool) ([]procfile.ProcfileEntry, error) { - var entries []procfile.ProcfileEntry - text, err := procfile.GetProcfileContent(path) - if err != nil { - return entries, err - } +// The name of the cli tool +var AppName = "procfile-util" - return procfile.ParseProcfile(text, delimiter, strict) -} +// Holds the version +var Version string func main() { - parser := argparse.NewParser("procfile-util", "A procfile parsing tool") - procfileFlag := parser.String("P", "procfile", &argparse.Options{Default: "Procfile", Help: "path to a procfile"}) - delimiterFlag := parser.String("D", "delimiter", &argparse.Options{Default: ":", Help: "delimiter in use within procfile"}) - defaultPortFlag := parser.String("d", "default-port", &argparse.Options{Default: "5000", Help: "default port to use"}) - strictFlag := parser.Flag("S", "strict", &argparse.Options{Help: "strictly parse the Procfile"}) - versionFlag := parser.Flag("v", "version", &argparse.Options{Help: "show version"}) - - checkCmd := parser.NewCommand("check", "check that the specified procfile is valid") - - deleteCmd := parser.NewCommand("delete", "delete a process type from a file") - processTypeDeleteFlag := deleteCmd.String("p", "process-type", &argparse.Options{Help: "name of process to delete", Required: true}) - stdoutDeleteFlag := deleteCmd.Flag("s", "stdout", &argparse.Options{Help: "write output to stdout"}) - writePathDeleteFlag := deleteCmd.String("w", "write-path", &argparse.Options{Help: "path to Procfile to write to"}) - - existsCmd := parser.NewCommand("exists", "check if a process type exists") - processTypeExistsFlag := existsCmd.String("p", "process-type", &argparse.Options{Help: "name of process to retrieve"}) - - expandCmd := parser.NewCommand("expand", "expands a procfile against a specific environment") - allowGetenvExpandFlag := expandCmd.Flag("a", "allow-getenv", &argparse.Options{Help: "allow the use of the existing env when expanding commands"}) - envPathExpandFlag := expandCmd.String("e", "env-file", &argparse.Options{Help: "path to a dotenv file"}) - processTypeExpandFlag := expandCmd.String("p", "process-type", &argparse.Options{Help: "name of process to expand"}) - - exportCmd := parser.NewCommand("export", "export the application to another process management format") - appExportFlag := exportCmd.String("", "app", &argparse.Options{Default: "app", Help: "name of app"}) - descriptionExportFlag := exportCmd.String("", "description", &argparse.Options{Help: "process description"}) - envPathExportFlag := exportCmd.String("e", "env-file", &argparse.Options{Help: "path to a dotenv file"}) - formatExportFlag := exportCmd.String("", "format", &argparse.Options{Default: "systemd", Help: "format to export"}) - formationExportFlag := exportCmd.String("", "formation", &argparse.Options{Default: "all=1", Help: "specify what processes will run and how many"}) - groupExportFlag := exportCmd.String("", "group", &argparse.Options{Help: "group to run the command as"}) - homeExportFlag := exportCmd.String("", "home", &argparse.Options{Help: "home directory for program"}) - limitCoredumpExportFlag := exportCmd.String("", "limit-coredump", &argparse.Options{Help: "Largest size (in blocks) of a core file that can be created. (setrlimit RLIMIT_CORE)"}) - limitCputimeExportFlag := exportCmd.String("", "limit-cputime", &argparse.Options{Help: "Maximum amount of cpu time (in seconds) a program may use. (setrlimit RLIMIT_CPU)"}) - limitDataExportFlag := exportCmd.String("", "limit-data", &argparse.Options{Help: "Maximum data segment size (setrlimit RLIMIT_DATA)"}) - limitFileSizeExportFlag := exportCmd.String("", "limit-file-size", &argparse.Options{Help: "Maximum size (in blocks) of a file receiving writes (setrlimit RLIMIT_FSIZE)"}) - limitLockedMemoryExportFlag := exportCmd.String("", "limit-locked-memory", &argparse.Options{Help: "Maximum amount of memory (in bytes) lockable with mlock(2) (setrlimit RLIMIT_MEMLOCK)"}) - limitOpenFilesExportFlag := exportCmd.String("", "limit-open-files", &argparse.Options{Help: "maximum number of open files, sockets, etc. (setrlimit RLIMIT_NOFILE)"}) - limitUserProcessesExportFlag := exportCmd.String("", "limit-user-processes", &argparse.Options{Help: "Maximum number of running processes (or threads!) for this user id. Not recommended because this setting applies to the user, not the process group. (setrlimit RLIMIT_NPROC)"}) - limitPhysicalMemoryExportFlag := exportCmd.String("", "limit-physical-memory", &argparse.Options{Help: "Maximum resident set size (in bytes); the amount of physical memory used by a process. (setrlimit RLIMIT_RSS)"}) - limitStackSizeExportFlag := exportCmd.String("", "limit-stack-size", &argparse.Options{Help: "Maximum size (in bytes) of a stack segment (setrlimit RLIMIT_STACK)"}) - locationExportFlag := exportCmd.String("", "location", &argparse.Options{Help: "location to output to"}) - logPathExportFlag := exportCmd.String("", "log-path", &argparse.Options{Default: "/var/log", Help: "log directory"}) - niceExportFlag := exportCmd.String("", "nice", &argparse.Options{Help: "nice level to add to this program before running"}) - prestartExportFlag := exportCmd.String("", "prestart", &argparse.Options{Help: "A command to execute before starting and restarting. A failure of this command will cause the start/restart to abort. This is useful for health checks, config tests, or similar operations."}) - workingDirectoryPathExportFlag := exportCmd.String("", "working-directory-path", &argparse.Options{Default: "/", Help: "working directory path for app"}) - runExportFlag := exportCmd.String("", "run", &argparse.Options{Help: "run pid file directory, defaults to /var/run/"}) - timeoutExportFlag := exportCmd.Int("", "timeout", &argparse.Options{Default: 5, Help: "amount of time (in seconds) processes have to shutdown gracefully before receiving a SIGKILL"}) - userExportFlag := exportCmd.String("", "user", &argparse.Options{Help: "user to run the command as"}) - - listCmd := parser.NewCommand("list", "list all process types in a procfile") - - setCmd := parser.NewCommand("set", "set the command for a process type in a file") - processTypeSetFlag := setCmd.String("p", "process-type", &argparse.Options{Help: "name of process to set", Required: true}) - commandSetFlag := setCmd.String("c", "command", &argparse.Options{Help: "command to set", Required: true}) - stdoutSetFlag := setCmd.Flag("s", "stdout", &argparse.Options{Help: "write output to stdout"}) - writePathSetFlag := setCmd.String("w", "write-path", &argparse.Options{Help: "path to Procfile to write to"}) - - showCmd := parser.NewCommand("show", "show the command for a specific process type") - allowGetenvShowFlag := showCmd.Flag("a", "allow-getenv", &argparse.Options{Help: "allow the use of the existing env when expanding commands"}) - envPathShowFlag := showCmd.String("e", "env-file", &argparse.Options{Help: "path to a dotenv file"}) - processTypeShowFlag := showCmd.String("p", "process-type", &argparse.Options{Help: "name of process to show", Required: true}) - - if err := parser.Parse(os.Args); err != nil { - fmt.Fprintf(os.Stderr, "%s\n", parser.Usage(err)) - os.Exit(1) - return - } - - if *versionFlag { - fmt.Printf("procfile-util %v\n", Version) - os.Exit(0) - return - } + os.Exit(Run(os.Args[1:])) +} - entries, err := parseProcfile(*procfileFlag, *delimiterFlag, *strictFlag) +// Executes the specified subcommand +func Run(args []string) int { + ctx := context.Background() + commandMeta := command.SetupRun(ctx, AppName, Version, args) + commandMeta.Ui = command.HumanZerologUiWithFields(commandMeta.Ui, make(map[string]interface{}, 0)) + c := cli.NewCLI(AppName, Version) + c.Args = os.Args[1:] + c.Commands = command.Commands(ctx, commandMeta, Commands) + exitCode, err := c.Run() if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) - os.Exit(1) - return - } - - defaultPort := 5000 - if *defaultPortFlag != "" { - i, err := strconv.Atoi(*defaultPortFlag) - if err != nil { - fmt.Fprintf(os.Stderr, "Invalid default port value: %v\n", err) - os.Exit(1) - return - } - defaultPort = i + fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error()) + return 1 } - success := false - if checkCmd.Happened() { - success = commands.CheckCommand(entries) - } else if deleteCmd.Happened() { - success = commands.DeleteCommand(entries, *processTypeDeleteFlag, *writePathDeleteFlag, *stdoutDeleteFlag, *delimiterFlag, *procfileFlag) - } else if existsCmd.Happened() { - success = commands.ExistsCommand(entries, *processTypeExistsFlag) - } else if expandCmd.Happened() { - success = commands.ExpandCommand(entries, *envPathExpandFlag, *allowGetenvExpandFlag, *processTypeExpandFlag, defaultPort, *delimiterFlag) - } else if exportCmd.Happened() { - success = commands.ExportCommand(entries, *appExportFlag, *descriptionExportFlag, *envPathExportFlag, *formatExportFlag, *formationExportFlag, *groupExportFlag, *homeExportFlag, *limitCoredumpExportFlag, *limitCputimeExportFlag, *limitDataExportFlag, *limitFileSizeExportFlag, *limitLockedMemoryExportFlag, *limitOpenFilesExportFlag, *limitUserProcessesExportFlag, *limitPhysicalMemoryExportFlag, *limitStackSizeExportFlag, *locationExportFlag, *logPathExportFlag, *niceExportFlag, *prestartExportFlag, *workingDirectoryPathExportFlag, *runExportFlag, *timeoutExportFlag, *userExportFlag, defaultPort) - } else if listCmd.Happened() { - success = commands.ListCommand(entries) - } else if setCmd.Happened() { - success = commands.SetCommand(entries, *processTypeSetFlag, *commandSetFlag, *writePathSetFlag, *stdoutSetFlag, *delimiterFlag, *procfileFlag) - } else if showCmd.Happened() { - success = commands.ShowCommand(entries, *envPathShowFlag, *allowGetenvShowFlag, *processTypeShowFlag, defaultPort) - } else { - fmt.Print(parser.Usage(err)) - } + return exitCode +} - if !success { - os.Exit(1) +// Returns a list of implemented commands +func Commands(ctx context.Context, meta command.Meta) map[string]cli.CommandFactory { + return map[string]cli.CommandFactory{ + "check": func() (cli.Command, error) { + return &commands.CheckCommand{Meta: meta}, nil + }, + "delete": func() (cli.Command, error) { + return &commands.DeleteCommand{Meta: meta}, nil + }, + "exists": func() (cli.Command, error) { + return &commands.ExistsCommand{Meta: meta}, nil + }, + "expand": func() (cli.Command, error) { + return &commands.ExpandCommand{Meta: meta}, nil + }, + "export": func() (cli.Command, error) { + return &commands.ExportCommand{Meta: meta}, nil + }, + "list": func() (cli.Command, error) { + return &commands.ListCommand{Meta: meta}, nil + }, + "set": func() (cli.Command, error) { + return &commands.SetCommand{Meta: meta}, nil + }, + "show": func() (cli.Command, error) { + return &commands.ShowCommand{Meta: meta}, nil + }, + "version": func() (cli.Command, error) { + return &command.VersionCommand{Meta: meta}, nil + }, } } diff --git a/procfile/io.go b/procfile/io.go index 03d0dc6..ac511a0 100644 --- a/procfile/io.go +++ b/procfile/io.go @@ -2,6 +2,7 @@ package procfile import ( "bufio" + "errors" "fmt" "io/ioutil" "os" @@ -35,10 +36,9 @@ func GetProcfileContent(path string) (string, error) { return strings.Join(lines, "\n"), err } -func OutputProcfile(path string, writePath string, delimiter string, stdout bool, entries []ProcfileEntry) bool { +func OutputProcfile(path string, writePath string, delimiter string, stdout bool, entries []ProcfileEntry) error { if writePath != "" && stdout { - fmt.Fprintf(os.Stderr, "cannot specify both --stdout and --write-path flags\n") - return false + return errors.New("cannot specify both --stdout and --write-path flags") } sort.Slice(entries, func(i, j int) bool { @@ -49,7 +49,7 @@ func OutputProcfile(path string, writePath string, delimiter string, stdout bool for _, entry := range entries { fmt.Printf("%v%v %v\n", entry.Name, delimiter, entry.Command) } - return true + return nil } if writePath != "" { @@ -57,11 +57,10 @@ func OutputProcfile(path string, writePath string, delimiter string, stdout bool } if err := writeProcfile(path, delimiter, entries); err != nil { - fmt.Fprintf(os.Stderr, "error writing procfile: %s\n", err) - return false + return fmt.Errorf("error writing procfile: %s", err) } - return true + return nil } func writeProcfile(path string, delimiter string, entries []ProcfileEntry) error { diff --git a/procfile/parse.go b/procfile/parse.go index 72f8c15..5f8900b 100644 --- a/procfile/parse.go +++ b/procfile/parse.go @@ -38,6 +38,7 @@ func ParseProcfile(text string, delimiter string, strict bool) ([]ProcfileEntry, reOldCmd, _ := regexp.Compile(`^([A-Za-z0-9_-]+)` + delimiter + `\s*(.+)$`) reComment, _ := regexp.Compile(`^(.*)\s#.+$`) + reForwardslashComment, _ := regexp.Compile(`^(.*)\s//.+$`) lineNumber := 0 names := make(map[string]bool) @@ -55,7 +56,7 @@ func ParseProcfile(text string, delimiter string, strict bool) ([]ProcfileEntry, params := reCmd.FindStringSubmatch(line) isCommand := len(params) == 4 isOldCommand := len(oldParams) == 3 - isComment := strings.HasPrefix(line, "#") + isComment := strings.HasPrefix(line, "#") || strings.HasPrefix(line, "//") if isComment { continue } @@ -88,12 +89,15 @@ func ParseProcfile(text string, delimiter string, strict bool) ([]ProcfileEntry, names[name] = true commentParams := reComment.FindStringSubmatch(cmd) + reForwardslashCommentParams := reForwardslashComment.FindStringSubmatch(cmd) if len(commentParams) == 2 { cmd = commentParams[1] + } else if len(reForwardslashCommentParams) == 2 { + cmd = reForwardslashCommentParams[1] } cmd = strings.TrimSpace(cmd) - if strings.HasPrefix(cmd, "#") { + if strings.HasPrefix(cmd, "#") || strings.HasPrefix(cmd, "//") { return entries, fmt.Errorf("comment specified in place of command, line %d", lineNumber) } diff --git a/test.bats b/test.bats index 788ad14..81edc96 100644 --- a/test.bats +++ b/test.bats @@ -16,7 +16,21 @@ teardown_file() { echo "output: $output" echo "status: $status" [[ "$status" -eq 0 ]] - [[ "$output" == "valid procfile detected 2custom, cron, custom, release, web, wor-ker" ]] + assert_output_contains "valid procfile detected 2custom, cron, custom, release, web, wor-ker" +} + +@test "[lax] forwardslash-comments" { + run $PROCFILE_BIN check -P fixtures/forwardslash-comments.Procfile + echo "output: $output" + echo "status: $status" + [[ "$status" -eq 0 ]] + assert_output_contains "valid procfile detected web, worker, worker-2" + + run $PROCFILE_BIN show -P fixtures/forwardslash-comments.Procfile -p worker + echo "output: $output" + echo "status: $status" + [[ "$status" -eq 0 ]] + assert_output_contains "node worker.js" } @test "[lax] multiple" { @@ -24,7 +38,7 @@ teardown_file() { echo "output: $output" echo "status: $status" [[ "$status" -eq 0 ]] - [[ "$output" == "valid procfile detected release, web, webpacker, worker" ]] + assert_output_contains "valid procfile detected release, web, webpacker, worker" } @test "[lax] port" { @@ -32,13 +46,13 @@ teardown_file() { echo "output: $output" echo "status: $status" [[ "$status" -eq 0 ]] - [[ "$output" == "valid procfile detected web, worker" ]] + assert_output_contains "valid procfile detected web, worker" run $PROCFILE_BIN show -P fixtures/port.Procfile -p web echo "output: $output" echo "status: $status" [[ "$status" -eq 0 ]] - [[ "$output" == "node web.js --port 5000" ]] + assert_output_contains "node web.js --port 5000" } @test "[strict] comments" { @@ -46,7 +60,21 @@ teardown_file() { echo "output: $output" echo "status: $status" [[ "$status" -eq 0 ]] - [[ "$output" == "valid procfile detected 2custom, cron, custom, release, web, wor-ker" ]] + assert_output_contains "valid procfile detected 2custom, cron, custom, release, web, wor-ker" +} + +@test "[strict] forwardslash-comments" { + run $PROCFILE_BIN check -S -P fixtures/forwardslash-comments.Procfile + echo "output: $output" + echo "status: $status" + [[ "$status" -eq 0 ]] + assert_output_contains "valid procfile detected web, worker, worker-2" + + run $PROCFILE_BIN show -S -P fixtures/forwardslash-comments.Procfile -p worker + echo "output: $output" + echo "status: $status" + [[ "$status" -eq 0 ]] + assert_output_contains "node worker.js" } @test "[strict] multiple" { @@ -54,7 +82,7 @@ teardown_file() { echo "output: $output" echo "status: $status" [[ "$status" -eq 0 ]] - [[ "$output" == "valid procfile detected release, web, webpacker, worker" ]] + assert_output_contains "valid procfile detected release, web, webpacker, worker" } @test "[strict] port" { @@ -62,11 +90,84 @@ teardown_file() { echo "output: $output" echo "status: $status" [[ "$status" -eq 0 ]] - [[ "$output" == "valid procfile detected web, worker" ]] + assert_output_contains "valid procfile detected web, worker" run $PROCFILE_BIN show -S -P fixtures/port.Procfile -p web echo "output: $output" echo "status: $status" [[ "$status" -eq 0 ]] - [[ "$output" == "node web.js --port 5000" ]] + assert_output_contains "node web.js --port 5000" +} + +flunk() { + { + if [[ "$#" -eq 0 ]]; then + cat - + else + echo "$*" + fi + } + return 1 +} + +assert_equal() { + if [[ "$1" != "$2" ]]; then + { + echo "expected: $1" + echo "actual: $2" + } | flunk + fi +} + +assert_exit_status() { + exit_status="$1" + if [[ "$status" -ne "$exit_status" ]]; then + { + echo "expected exit status: $exit_status" + echo "actual exit status: $status" + } | flunk + flunk + fi +} + +assert_failure() { + if [[ "$status" -eq 0 ]]; then + flunk "expected failed exit status" + elif [[ "$#" -gt 0 ]]; then + assert_output "$1" + fi +} + +assert_success() { + if [[ "$status" -ne 0 ]]; then + flunk "command failed with exit status $status" + elif [[ "$#" -gt 0 ]]; then + assert_output "$1" + fi +} + +assert_output() { + local expected + if [[ $# -eq 0 ]]; then + expected="$(cat -)" + else + expected="$1" + fi + assert_equal "$expected" "$output" +} + +assert_output_contains() { + local input="$output" + local expected="$1" + local count="${2:-1}" + local found=0 + until [ "${input/$expected/}" = "$input" ]; do + input="${input/$expected/}" + found=$((found + 1)) + done + assert_equal "$count" "$found" +} + +assert_output_not_exists() { + [[ -z "$output" ]] || flunk "expected no output, found some" }