Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
softwarespot committed Nov 17, 2024
0 parents commit 9079e73
Show file tree
Hide file tree
Showing 36 changed files with 1,818 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go
on: [push, pull_request]

jobs:
test:
strategy:
matrix:
go-version: [1.23.x]

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Test
run: go test -cover -v ./...
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Taken from URL: https://github.com/github/gitignore/blob/main/Go.gitignore
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum

# env file
.env

# Misc
bin/
porty_db.json
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 SoftwareSpot Apps

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# If the "VERSION" environment variable is not set, then use this value instead
VERSION?=1.0.0
TIME=$(shell date +%FT%T%z)
GOVERSION=$(shell go version | awk '{print $$3}' | sed s/go//)

LDFLAGS=-ldflags "\
-X github.com/softwarespot/porty/internal/version.Version=${VERSION} \
-X github.com/softwarespot/porty/internal/version.Time=${TIME} \
-X github.com/softwarespot/porty/internal/version.User=${USER} \
-X github.com/softwarespot/porty/internal/version.GoVersion=${GOVERSION} \
-s \
-w \
"

build:
@echo building to bin/porty
@go build $(LDFLAGS) -o ./bin/porty

install:
@echo copying bin/porty to $(HOME)/bin/porty
@mv ./bin/porty $(HOME)/bin/porty

test:
@go test -cover -v ./...

.PHONY: build install test
150 changes: 150 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Porty

![Go Tests](https://github.com/softwarespot/porty/actions/workflows/go.yml/badge.svg)

## Prerequisites

- go 1.23.0 or above
- make (if you want to use the `Makefile` provided)

## Installation

Build the binary `porty` executable to the directory `./bin` i.e. `./bin/porty`.

```bash
make
```

Install i.e. copy the executable `./bin/porty` to `$HOME/bin` (if it exists).

```bash
make install
```

Initialize the ports database.

```bash
porty init
```

### Init

Initialize the ports database.

```bash
porty init
```

### Get

Get or assign the next available port number for the app name.

```bash
porty get myapp
```

### List

List all port numbers.

```bash
porty list
```

### Next

Get the next available port number, without assigning to an app name.

```bash
porty next
```

### Remove

Remove a port number for the app name.

```bash
porty remove myapp
```

### Who

Get who has been assigned to a port number.

```bash
porty who 8001
```

### Version

Display the version of the application.

```bash
porty version
```

### Help

Display the help text and exit.

```bash
porty --help
```

## Autocompletion

Adds autocompletion for the application's commands.

Add the following line to the `.bashrc` file; otherwise, refer to [kubectl's](https://kubernetes.io/docs/tasks/tools/install-kubectl/#optional-kubectl-configurations) documentation about locations for other OSs

- For `bash`

```bash
source <(porty completion bash)
```

- For `fish`

```bash
source <(porty completion fish)
```

- For `zsh`

```bash
source <(porty completion zsh)
```

### Example (bash)

Example of adding autocompletion for `bash` to the `.bashrc` file

```bash
echo 'source <(porty completion bash)' >> ~/.bashrc
source ~/.bashrc
```

## Dependencies

**IMPORTANT:** 3rd party dependencies are used.

I only ever use dependencies when it's say an adapter for
an external service e.g. Redis, MySQL or Prometheus.

## Linting

Docker

```bash
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:latest golangci-lint run -v --tests=false --disable-all -E durationcheck,errorlint,exhaustive,gocritic,gosimple,ineffassign,misspell,predeclared,revive,staticcheck,unparam,unused,whitespace --max-issues-per-linter=10000 --max-same-issues=10000
```

Local

```bash
golangci-lint run --tests=false --disable-all -E durationcheck,errorlint,exhaustive,gocritic,gosimple,ineffassign,misspell,predeclared,revive,staticcheck,unparam,unused,whitespace --max-issues-per-linter=10000 --max-same-issues=10000
```

## License

The code has been licensed under the [MIT](https://opensource.org/license/mit) license.
56 changes: 56 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"fmt"
"io"
"os"

"github.com/softwarespot/porty/internal/helpers"
"github.com/spf13/cobra"
)

func newCompletionCmd() *cobra.Command {
byShellFuncs := map[string]func(cmd *cobra.Command, out io.Writer) error{
"bash": func(cmd *cobra.Command, w io.Writer) error {
return cmd.Root().GenBashCompletion(w)
},
"fish": func(cmd *cobra.Command, w io.Writer) error {
return cmd.Root().GenFishCompletion(w, true)
},
"zsh": func(cmd *cobra.Command, w io.Writer) error {
return cmd.Root().GenZshCompletion(w)
},
}

var shells []string
for s := range byShellFuncs {
shells = append(shells, s)
}

shellList := helpers.ShellQuoteJoin(shells)

return &cobra.Command{
Use: "completion SHELL",
Short: fmt.Sprintf("Generate autocompletions script for the specified shell (%s)", shellList),
Long: expandExecutable(`Generate autocompletions script for the specified shells (%s)
This command generates shell autocompletions for bash e.g.
$ <EXE> completion bash
Can be sourced as such in .bashrc
$ source <(<EXE> completion bash)`, shellList),
ValidArgs: shells,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("shell must be defined, expected one of the following: %s", shellList)
}

shell := args[0]
fn, ok := byShellFuncs[shell]
if !ok {
return fmt.Errorf("invalid shell of %q was provided, expected one of the following: %s", shell, shellList)
}
return fn(cmd, os.Stdout)
},
}
}
48 changes: 48 additions & 0 deletions cmd/completion_args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"github.com/softwarespot/porty/internal/ports"
"github.com/spf13/cobra"
)

func createCompleteAppNames(opts *cliOptions) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

var appnames []string

// Ignore the error
runPortsFunc(opts, func(username string, m *ports.Manager) error {
ups, err := m.AllByUsername(username, ports.SortByUsernameAppName)
if err != nil {
return err
}
for _, up := range ups {
appnames = append(appnames, up.AppName)
}
return nil
})
return appnames, cobra.ShellCompDirectiveNoFileComp
}
}

func createCompletePorts(opts *cliOptions) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

var ps []string

// Ignore the error
runPortsFunc(opts, func(_ string, m *ports.Manager) error {
for _, up := range m.All(ports.SortByUsernameAppName) {
ps = append(ps, up.Port.String())
}
return nil
})
return ps, cobra.ShellCompDirectiveNoFileComp
}
}
Loading

0 comments on commit 9079e73

Please sign in to comment.