From 903ed01764da07699a79da98d13d75bc19b91704 Mon Sep 17 00:00:00 2001 From: "Alex Munene (@enenumxela)" <62714471+enenumxela@users.noreply.github.com> Date: Tue, 24 Oct 2023 02:45:47 +0300 Subject: [PATCH] chore: - --- .github/workflows/codeql-analysis.yml | 48 ++++ .github/workflows/linting.yml | 43 ++++ .github/workflows/{test.yml => testing.yml} | 18 +- .gitignore | 3 - .golangci.yaml | 183 +++++++++++---- CONTRIBUTING.md | 4 +- Makefile | 40 +++- README.md | 2 +- go.mod | 13 -- go.sum | 33 --- log/README.md | 3 - log/event.go | 46 ---- log/formatter/cli.go | 79 ------- log/formatter/formatter.go | 13 -- log/levels/levels.go | 22 -- log/logger.go | 144 ------------ log/main.go | 90 -------- log/streamer.go | 145 ------------ log/utils.go | 18 -- log/writer/cli.go | 35 --- log/writer/writer.go | 7 - ratelimiter/README.md | 40 ---- ratelimiter/doc.go | 2 - ratelimiter/ratelimiter.go | 61 ----- ssh/README.md | 142 ------------ ssh/authentication/authentication.go | 45 ---- ssh/command.go | 12 - ssh/keys/keys.go | 147 ------------ ssh/sftp.go | 236 -------------------- ssh/ssh.go | 235 ------------------- url/README.md | 9 - url/url.go | 117 ---------- url/url_test.go | 142 ------------ 33 files changed, 282 insertions(+), 1895 deletions(-) create mode 100644 .github/workflows/codeql-analysis.yml create mode 100644 .github/workflows/linting.yml rename .github/workflows/{test.yml => testing.yml} (71%) delete mode 100644 log/README.md delete mode 100644 log/event.go delete mode 100644 log/formatter/cli.go delete mode 100644 log/formatter/formatter.go delete mode 100644 log/levels/levels.go delete mode 100644 log/logger.go delete mode 100644 log/main.go delete mode 100644 log/streamer.go delete mode 100644 log/utils.go delete mode 100644 log/writer/cli.go delete mode 100644 log/writer/writer.go delete mode 100644 ratelimiter/README.md delete mode 100644 ratelimiter/doc.go delete mode 100644 ratelimiter/ratelimiter.go delete mode 100644 ssh/README.md delete mode 100644 ssh/authentication/authentication.go delete mode 100644 ssh/command.go delete mode 100644 ssh/keys/keys.go delete mode 100644 ssh/sftp.go delete mode 100644 ssh/ssh.go delete mode 100644 url/README.md delete mode 100644 url/url.go delete mode 100644 url/url_test.go diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..ce0eb54 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,48 @@ +name: ๐Ÿšจ CodeQL Analysis + +on: + push: + branches: + - "main" + paths: + - '**.go' + - '**.mod' + pull_request: + branches: + - "main" + paths: + - '**.go' + - '**.mod' + workflow_dispatch: + +jobs: + analyze: + name: Analyze + strategy: + fail-fast: false + matrix: + language: [ 'go' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + steps: + - + name: Checkout the repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + # Initializes the CodeQL tools for scanning. + - + name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + - + name: Autobuild + uses: github/codeql-action/autobuild@v2 + - + name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml new file mode 100644 index 0000000..f7d4494 --- /dev/null +++ b/.github/workflows/linting.yml @@ -0,0 +1,43 @@ +name: ๐Ÿ’… Linting + +on: + push: + branches: + - "main" + paths: + - '**.go' + - '**.mod' + pull_request: + branches: + - "main" + paths: + - '**.go' + - '**.mod' + workflow_dispatch: + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + steps: + - + name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '>=1.20' + - + name: Checkout the repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - + name: Run golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.52.2 + args: --timeout 5m + working-directory: . \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/testing.yml similarity index 71% rename from .github/workflows/test.yml rename to .github/workflows/testing.yml index 154e9f1..8be7c74 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/testing.yml @@ -1,10 +1,18 @@ -name: ๐Ÿงช test +name: ๐Ÿงช Testing on: push: - branches: [ dev ] + branches: + - "main" + paths: + - '**.go' + - '**.mod' pull_request: - branches: [ dev ] + branches: + - "main" + paths: + - '**.go' + - '**.mod' workflow_dispatch: jobs: @@ -21,8 +29,8 @@ jobs: with: go-version: '>=1.20' - - name: Checkout the code - uses: actions/checkout@v3 + name: Checkout the repository + uses: actions/checkout@v4 with: fetch-depth: 0 - diff --git a/.gitignore b/.gitignore index c686ba1..e69de29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +0,0 @@ -# Notes - -notes.txt \ No newline at end of file diff --git a/.golangci.yaml b/.golangci.yaml index 5de582c..2e03ac5 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,61 +1,160 @@ run: - issues-exit-code: 1 + # Timeout for analysis, e.g. 30s, 5m. + # Default: 1m + timeout: 5m linters: + # Disable all linters. + # Default: false disable-all: true + # Enable specific linter enable: - - bodyclose - - depguard - - dogsled - - dupl - - errcheck - - exportloopref - - exhaustive - - goconst - - gocritic - - gofmt - - goimports - - gocyclo - - gosec - - gosimple - - govet - - ineffassign - - misspell - - nolintlint - - prealloc - - predeclared - - revive - - staticcheck - - stylecheck - - thelper - - tparallel - - typecheck - - unconvert - - unparam - - unused - - whitespace - - wsl + # Enabled by Default + - errcheck # errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false] + - gosimple # (megacheck) # Linter for Go source code that specializes in simplifying code [fast: false, auto-fix: false] + - govet # (vet, vetshadow) # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false] + - ineffassign # Detects when assignments to existing variables are not used [fast: true, auto-fix: false] + - staticcheck # (megacheck) # It's a set of rules from staticcheck. It's not the same thing as the staticcheck binary. The author of staticcheck doesn't support or approve the use of staticcheck as a library inside golangci-lint. [fast: false, auto-fix: false] + - unused # (megacheck) # Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false] + # Disabled by Default + - asasalint # check for pass []any as any in variadic func(...any) [fast: false, auto-fix: false] + - asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers [fast: true, auto-fix: false] + - bidichk # Checks for dangerous unicode character sequences [fast: true, auto-fix: false] + - bodyclose # checks whether HTTP response body is closed successfully [fast: false, auto-fix: false] + - containedctx # containedctx is a linter that detects struct contained context.Context field [fast: false, auto-fix: false] + - contextcheck # check whether the function uses a non-inherited context [fast: false, auto-fix: false] + # - cyclop # checks function and package cyclomatic complexity [fast: false, auto-fix: false] + # - deadcode # [deprecated] # Finds unused code [fast: false, auto-fix: false] + - decorder # check declaration order and count of types, constants, variables and functions [fast: true, auto-fix: false] + # - depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false] + - dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false] + # - dupl # Tool for code clone detection [fast: true, auto-fix: false] + - dupword # checks for duplicate words in the source code [fast: true, auto-fix: true] + - durationcheck # check for two durations multiplied together [fast: false, auto-fix: false] + - errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted. [fast: false, auto-fix: false] + - errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. [fast: false, auto-fix: false] + - errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false] + - execinquery # execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds [fast: false, auto-fix: false] + - exhaustive # check exhaustiveness of enum switch statements [fast: false, auto-fix: false] + # - exhaustivestruct # [deprecated] # Checks if all struct's fields are initialized [fast: false, auto-fix: false] + # - exhaustruct # Checks if all structure fields are initialized [fast: false, auto-fix: false] + - exportloopref # checks for pointers to enclosing loop variables [fast: false, auto-fix: false] + # - forbidigo # Forbids identifiers [fast: false, auto-fix: false] + - forcetypeassert # finds forced type assertions [fast: true, auto-fix: false] + # - funlen # Tool for detection of long functions [fast: true, auto-fix: false] + - gci # Gci controls Go package import order and makes it always deterministic. [fast: true, auto-fix: false] + - ginkgolinter # enforces standards of using ginkgo and gomega [fast: false, auto-fix: false] + - gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false] + # - gochecknoglobals # check that no global variables exist [fast: false, auto-fix: false] + # - gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false] + # - gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false] + - goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false] + - gocritic # Provides diagnostics that check for bugs, performance and style issues. [fast: false, auto-fix: false] + # - gocyclo # Computes and checks the cyclomatic complexity of functions [fast: true, auto-fix: false] + # - godot # Check if comments end in a period [fast: true, auto-fix: true] + # - godox # Tool for detection of FIXME, TODO and other comment keywords [fast: true, auto-fix: false] + # - goerr113 # Go linter to check the errors handling expressions [fast: false, auto-fix: false] + - gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true] + - gofumpt # Gofumpt checks whether code was gofumpt-ed. [fast: true, auto-fix: true] + - goheader # Checks is file header matches to pattern [fast: true, auto-fix: false] + - goimports # Check import statements are formatted according to the 'goimport' command. Reformat imports in autofix mode. [fast: true, auto-fix: true] + # - golint # [deprecated] # Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: false, auto-fix: false] + # - gomnd # An analyzer to detect magic numbers. [fast: true, auto-fix: false] + - gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod. [fast: true, auto-fix: false] + - gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. [fast: true, auto-fix: false] + - goprintffuncname # Checks that printf-like functions are named with `f` at the end [fast: true, auto-fix: false] + # - gosec # (gas) # Inspects source code for security problems [fast: false, auto-fix: false] + # - gosmopolitan # Report certain i18n/l10n anti-patterns in your Go codebase [fast: false, auto-fix: false] + - grouper # An analyzer to analyze expression groups. [fast: true, auto-fix: false] + # - ifshort # [deprecated] # Checks that your code uses short syntax for if-statements whenever possible [fast: true, auto-fix: false] + - importas # Enforces consistent import aliases [fast: false, auto-fix: false] + - interfacebloat # A linter that checks the number of methods inside an interface. [fast: true, auto-fix: false] + # - interfacer # [deprecated] # Linter that suggests narrower interface types [fast: false, auto-fix: false] + # - ireturn # Accept Interfaces, Return Concrete Types [fast: false, auto-fix: false] + # - lll # Reports long lines [fast: true, auto-fix: false] + - loggercheck # (logrlint) # Checks key value pairs for common logger libraries (kitlog,klog,logr,zap). [fast: false, auto-fix: false] + - maintidx # maintidx measures the maintainability index of each function. [fast: true, auto-fix: false] + - makezero # Finds slice declarations with non-zero initial length [fast: false, auto-fix: false] + # - maligned # [deprecated] # Tool to detect Go structs that would take less memory if their fields were sorted [fast: false, auto-fix: false] + # - mirror # reports wrong mirror patterns of bytes/strings usage [fast: false, auto-fix: false] + - misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true] + - musttag # enforce field tags in (un)marshaled structs [fast: false, auto-fix: false] + # - nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false] + - nestif # Reports deeply nested if statements [fast: true, auto-fix: false] + - nilerr # Finds the code that returns nil even if it checks that the error is not nil. [fast: false, auto-fix: false] + - nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value. [fast: false, auto-fix: false] + - nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity [fast: true, auto-fix: false] + - noctx # noctx finds sending http request without context.Context [fast: false, auto-fix: false] + - nolintlint # Reports ill-formed or insufficient nolint directives [fast: true, auto-fix: false] + # - nonamedreturns # Reports all named returns [fast: false, auto-fix: false] + # - nosnakecase # [deprecated] # nosnakecase is a linter that detects snake case of variable naming and function name. [fast: true, auto-fix: false] + - nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL. [fast: true, auto-fix: false] + - paralleltest # paralleltest detects missing usage of t.Parallel() method in your Go test [fast: false, auto-fix: false] + - prealloc # Finds slice declarations that could potentially be pre-allocated [fast: true, auto-fix: false] + - predeclared # find code that shadows one of Go's predeclared identifiers [fast: true, auto-fix: false] + # - promlinter # Check Prometheus metrics naming via promlint [fast: true, auto-fix: false] + - reassign # Checks that package variables are not reassigned [fast: false, auto-fix: false] + - revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. [fast: false, auto-fix: false] + - rowserrcheck # checks whether Err of rows is checked successfully [fast: false, auto-fix: false] + # - scopelint # [deprecated] # Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false] + - sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed. [fast: false, auto-fix: false] + # - structcheck # [deprecated] # Finds unused struct fields [fast: false, auto-fix: false] + - stylecheck # Stylecheck is a replacement for golint [fast: false, auto-fix: false] + # - tagalign # check that struct tags are well aligned [fast: true, auto-fix: true] + # - tagliatelle # Checks the struct tags. [fast: true, auto-fix: false] + - tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 [fast: false, auto-fix: false] + - testableexamples # linter checks if examples are testable (have an expected output) [fast: true, auto-fix: false] + - testpackage # linter that makes you use a separate _test package [fast: true, auto-fix: false] + - thelper # thelper detects Go test helpers without t.Helper() call and checks the consistency of test helpers [fast: false, auto-fix: false] + - tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes [fast: false, auto-fix: false] + - unconvert # Remove unnecessary type conversions [fast: false, auto-fix: false] + - unparam # Reports unused function parameters [fast: false, auto-fix: false] + - usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library. [fast: true, auto-fix: false] + # - varcheck # [deprecated] # Finds unused global variables and constants [fast: false, auto-fix: false] + # - varnamelen # checks that the length of a variable's name matches its scope [fast: false, auto-fix: false] + - wastedassign # wastedassign finds wasted assignment statements. [fast: false, auto-fix: false] + - whitespace # Tool for detection of leading and trailing whitespace [fast: true, auto-fix: true] + # - wrapcheck # Checks that errors returned from external packages are wrapped [fast: false, auto-fix: false] + - wsl # Whitespace Linter - Forces you to use empty lines! [fast: true, auto-fix: false] + # - zerologlint # Detects the wrong usage of `zerolog` that a user forgets to dispatch with `Send` or `Msg`. [fast: false, auto-fix: false] linters-settings: - errcheck: - check-type-assertions: true goconst: min-len: 2 min-occurrences: 3 gocritic: enabled-tags: - - style - - diagnostic - performance - experimental + - style - opinionated disabled-checks: - captLocal - - octalLiteral + - whyNoLint + gocyclo: + # Minimal code complexity to report. + # Default: 30 (but we recommend 10-20) + min-complexity: 10 govet: check-shadowing: true - disabled-checks: - - fieldalignment - nolintlint: - require-explanation: true - require-specific: true \ No newline at end of file + varnamelen: + # The minimum length of a variable's name that is considered "long". + # Variable names that are at least this long will be ignored. + # Default: 3 + min-name-length: 2 + # Check method receivers. + # Default: false + check-receiver: true + # Check named return values. + # Default: false + check-return: true + # Check type parameters. + # Default: false + check-type-param: true + whitespace: + # Enforces newlines (or comments) after every multi-line if statement. + # Default: false + multi-if: true + # Enforces newlines (or comments) after every multi-line function signature. + # Default: false + multi-func: true \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c1d7a42..af9140f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -41,10 +41,10 @@ Pull requests should target the `dev` branch. Please also reference the issue fr When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. Here are a few points to keep in mind: -* Please run `go fmt ./...` before committing to ensure code aligns with go standards. -* We use [`golangci-lint`](https://golangci-lint.run/) for linting Go code, run `golangci-lint run --fix` before submitting PR. Editors such as Visual Studio Code or JetBrains IntelliJ; with Go support plugin will offer `golangci-lint` automatically. * All dependencies must be defined in the `go.mod` file. * Advanced IDEs and code editors (like VSCode) will take care of that, but to be sure, run `go mod tidy` to validate dependencies. +* Please run `go fmt ./...` before committing to ensure code aligns with go standards. +* We use [`golangci-lint`](https://golangci-lint.run/) for linting Go code, run `golangci-lint run --fix` before submitting PR. Editors such as Visual Studio Code or JetBrains IntelliJ; with Go support plugin will offer `golangci-lint` automatically. * For details on the approved style, check out [Effective Go](https://golang.org/doc/effective_go.html). ### License diff --git a/Makefile b/Makefile index a6614a3..888e27c 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,37 @@ -TARGET ?= hqgoutils -GO ?= go -GOFLAGS ?= +# Go(Golang) Options +GOCMD=go +GOMOD=$(GOCMD) mod +GOGET=$(GOCMD) get +GOFMT=$(GOCMD) fmt +GOTEST=$(GOCMD) test +GOFLAGS := -v +LDFLAGS := -s -w -fmt: - $(GO) $(GOFLAGS) fmt ./... +# Golangci Options +GOLANGCILINTCMD=golangci-lint +GOLANGCILINTRUN=$(GOLANGCILINTCMD) run +ifneq ($(shell go env GOOS),darwin) +LDFLAGS := -extldflags "-static" +endif + +.PHONY: tidy +tidy: + $(GOMOD) tidy + +.PHONY: update-deps +update-deps: + $(GOGET) -f -t -u ./... + $(GOGET) -f -u ./... + +.PHONY: format +format: + $(GOFMT) ./... + +.PHONY: lint +lint: + $(GOLANGCILINTRUN) ./... --fix + +.PHONY: test test: - $(GO) $(GOFLAGS) test ./... \ No newline at end of file + $(GOTEST) $(GOFLAGS) ./... \ No newline at end of file diff --git a/README.md b/README.md index 4a35e48..e5806f5 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Hueristiq's Go utilities. ## Contribution -[Issues](https://github.com/hueristiq/hqgoutils/issues) and [Pull Requests](https://github.com/hueristiq/hqgoutils/pulls) are welcome! Check out the [contribution guidelines.](./CONTRIBUTING.md) +[Issues](https://github.com/hueristiq/hqgoutils/issues) and [Pull Requests](https://github.com/hueristiq/hqgoutils/pulls) are welcome! Check out the [contribution guidelines.](https://github.com/hueristiq/hqgoutils/blob/master/LICENSE) ## Licensing diff --git a/go.mod b/go.mod index 0362d28..f0ec1e4 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,3 @@ module github.com/hueristiq/hqgoutils go 1.20 - -require ( - github.com/logrusorgru/aurora/v3 v3.0.0 - github.com/pkg/sftp v1.13.5 - golang.org/x/crypto v0.9.0 - golang.org/x/net v0.10.0 - golang.org/x/term v0.8.0 -) - -require ( - github.com/kr/fs v0.1.0 // indirect - golang.org/x/sys v0.8.0 // indirect -) diff --git a/go.sum b/go.sum index e8136dd..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,33 +0,0 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= -github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= -github.com/logrusorgru/aurora/v3 v3.0.0 h1:R6zcoZZbvVcGMvDCKo45A9U/lzYyzl5NfYIvznmDfE4= -github.com/logrusorgru/aurora/v3 v3.0.0/go.mod h1:vsR12bk5grlLvLXAYrBsb5Oc/N+LxAlxggSjiwMnCUc= -github.com/pkg/sftp v1.13.5 h1:a3RLUqkyjYRtBTZJZ1VRrKbN3zhuPLlUc3sphVz81go= -github.com/pkg/sftp v1.13.5/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg= -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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= -golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols= -golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/log/README.md b/log/README.md deleted file mode 100644 index 149f2af..0000000 --- a/log/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# hqgoutils/log - -A [Go(Golang)](https://golang.org/) package for logging. \ No newline at end of file diff --git a/log/event.go b/log/event.go deleted file mode 100644 index 9e4a9a3..0000000 --- a/log/event.go +++ /dev/null @@ -1,46 +0,0 @@ -package log - -import ( - "fmt" - - "github.com/hueristiq/hqgoutils/log/levels" -) - -type Event struct { - level levels.LevelInt - message string - metadata map[string]string - - logger *Logger -} - -func (event *Event) Label(label string) *Event { - event.metadata["label"] = label - - return event -} - -func (event *Event) Rest(character string) *Event { - event.metadata["rest"] = character - - return event -} - -// // Str adds a string metadata item to the log -// func (event *Event) Str(key, value string) *Event { -// event.metadata[key] = value - -// return event -// } - -func (event *Event) Msg(message string) { - event.message = message - - event.logger.Log(event) -} - -func (event *Event) Msgf(format string, args ...interface{}) { - event.message = fmt.Sprintf(format, args...) - - event.logger.Log(event) -} diff --git a/log/formatter/cli.go b/log/formatter/cli.go deleted file mode 100644 index 0a4ca46..0000000 --- a/log/formatter/cli.go +++ /dev/null @@ -1,79 +0,0 @@ -package formatter - -import ( - "bytes" - - "github.com/hueristiq/hqgoutils/log/levels" - "github.com/logrusorgru/aurora/v3" -) - -type CLI struct{} - -type CLIOptions struct { - Colorize bool -} - -var ( - _ Formatter = &CLI{} - au aurora.Aurora = aurora.NewAurora(false) -) - -func NewCLI(options *CLIOptions) *CLI { - au = aurora.NewAurora(options.Colorize) - - return &CLI{} -} - -func (cli *CLI) Format(event *Log) (data []byte, err error) { - cli.colorizeLabel(event) - - buffer := &bytes.Buffer{} - buffer.Grow(len(event.Message)) - - if label, ok := event.Metadata["label"]; ok && label != "" { - buffer.WriteRune('[') - buffer.WriteString(label) - buffer.WriteRune(']') - buffer.WriteRune(' ') - // delete(event.Metadata, "label") - } - - buffer.WriteString(event.Message) - - // for k, v := range event.Metadata { - // buffer.WriteRune(' ') - // buffer.WriteString(cli.colorizeKey(k)) - // buffer.WriteRune('=') - // buffer.WriteString(v) - // } - data = buffer.Bytes() - - return -} - -// func (cli *CLI) colorizeKey(key string) string { -// return au.Bold(key).String() -// } - -func (cli *CLI) colorizeLabel(event *Log) { - label := event.Metadata["label"] - - if label == "" { - return - } - - switch event.Level { - case levels.Levels[levels.LevelSilent]: - return - case levels.Levels[levels.LevelFatal]: - event.Metadata["label"] = au.BrightRed(label).Bold().String() - case levels.Levels[levels.LevelError]: - event.Metadata["label"] = au.BrightRed(label).Bold().String() - case levels.Levels[levels.LevelWarning]: - event.Metadata["label"] = au.BrightYellow(label).Bold().String() - case levels.Levels[levels.LevelInfo]: - event.Metadata["label"] = au.BrightBlue(label).Bold().String() - case levels.Levels[levels.LevelDebug]: - event.Metadata["label"] = au.BrightMagenta(label).Bold().String() - } -} diff --git a/log/formatter/formatter.go b/log/formatter/formatter.go deleted file mode 100644 index 88b66dd..0000000 --- a/log/formatter/formatter.go +++ /dev/null @@ -1,13 +0,0 @@ -package formatter - -import "github.com/hueristiq/hqgoutils/log/levels" - -type Log struct { - Message string - Level levels.LevelInt - Metadata map[string]string -} - -type Formatter interface { - Format(log *Log) ([]byte, error) -} diff --git a/log/levels/levels.go b/log/levels/levels.go deleted file mode 100644 index cb28ed1..0000000 --- a/log/levels/levels.go +++ /dev/null @@ -1,22 +0,0 @@ -package levels - -type LevelInt int -type LevelStr string - -const ( - LevelSilent LevelStr = "silent" - LevelFatal LevelStr = "fatal" - LevelError LevelStr = "error" - LevelWarning LevelStr = "warning" - LevelInfo LevelStr = "info" - LevelDebug LevelStr = "debug" -) - -var Levels = map[LevelStr]LevelInt{ - LevelSilent: LevelInt(0), - LevelFatal: LevelInt(1), - LevelError: LevelInt(2), - LevelWarning: LevelInt(3), - LevelInfo: LevelInt(4), - LevelDebug: LevelInt(5), -} diff --git a/log/logger.go b/log/logger.go deleted file mode 100644 index 40721b8..0000000 --- a/log/logger.go +++ /dev/null @@ -1,144 +0,0 @@ -package log - -import ( - "os" - "strings" - - "github.com/hueristiq/hqgoutils/log/formatter" - "github.com/hueristiq/hqgoutils/log/levels" - "github.com/hueristiq/hqgoutils/log/writer" -) - -type Logger struct { - formatter formatter.Formatter - maxLevel levels.LevelInt - writer writer.Writer -} - -func (logger *Logger) SetFormatter(formatter formatter.Formatter) { - logger.formatter = formatter -} - -func (logger *Logger) SetMaxLevel(level levels.LevelStr) { - logger.maxLevel = levels.Levels[level] -} - -func (logger *Logger) SetWriter(writer writer.Writer) { - logger.writer = writer -} - -func (logger *Logger) Log(event *Event) { - if event.level > logger.maxLevel { - return - } - - var ( - ok bool - label string - ) - - if _, ok = event.metadata["label"]; !ok { - labels := map[levels.LevelInt]string{ - levels.Levels[levels.LevelFatal]: "FTL", - levels.Levels[levels.LevelError]: "ERR", - levels.Levels[levels.LevelWarning]: "WRN", - levels.Levels[levels.LevelInfo]: "INF", - levels.Levels[levels.LevelDebug]: "DBG", - } - - if label, ok = labels[event.level]; ok { - event.metadata["label"] = label - } - } - - event.message = strings.TrimSuffix(event.message, "\n") - - data, err := logger.formatter.Format(&formatter.Log{ - Message: event.message, - Level: event.level, - Metadata: event.metadata, - }) - if err != nil { - return - } - - if character, ok := event.metadata["rest"]; ok { - data = appendRest(data, character) - } - - logger.writer.Write(data, event.level) - - if event.level == levels.Levels[levels.LevelFatal] { - os.Exit(1) - } -} - -func (logger *Logger) Print() *Event { - event := &Event{ - logger: logger, - level: levels.LevelInt(-1), - metadata: make(map[string]string), - } - - return event -} - -func (logger *Logger) Debug() *Event { - level := levels.Levels[levels.LevelDebug] - - event := &Event{ - logger: logger, - level: level, - metadata: make(map[string]string), - } - - return event -} - -func (logger *Logger) Info() *Event { - level := levels.Levels[levels.LevelInfo] - - event := &Event{ - logger: logger, - level: level, - metadata: make(map[string]string), - } - - return event -} - -func (logger *Logger) Warning() *Event { - level := levels.Levels[levels.LevelWarning] - - event := &Event{ - logger: logger, - level: level, - metadata: make(map[string]string), - } - - return event -} - -func (logger *Logger) Error() *Event { - level := levels.Levels[levels.LevelError] - - event := &Event{ - logger: logger, - level: level, - metadata: make(map[string]string), - } - - return event -} - -func (logger *Logger) Fatal() *Event { - level := levels.Levels[levels.LevelFatal] - - event := &Event{ - logger: logger, - level: level, - metadata: make(map[string]string), - } - - return event -} diff --git a/log/main.go b/log/main.go deleted file mode 100644 index 8962659..0000000 --- a/log/main.go +++ /dev/null @@ -1,90 +0,0 @@ -package log - -import ( - "github.com/hueristiq/hqgoutils/log/formatter" - "github.com/hueristiq/hqgoutils/log/levels" - "github.com/hueristiq/hqgoutils/log/writer" -) - -var ( - DefaultLogger *Logger -) - -func init() { - DefaultLogger = &Logger{} - DefaultLogger.SetMaxLevel(levels.LevelDebug) - DefaultLogger.SetFormatter(formatter.NewCLI(&formatter.CLIOptions{ - Colorize: true, - })) - DefaultLogger.SetWriter(writer.NewCLI()) -} - -func Print() (event *Event) { - event = &Event{ - logger: DefaultLogger, - level: levels.LevelInt(-1), - metadata: make(map[string]string), - } - - return event -} - -func Debug() (event *Event) { - level := levels.Levels[levels.LevelDebug] - - event = &Event{ - logger: DefaultLogger, - level: level, - metadata: make(map[string]string), - } - - return -} - -func Info() (event *Event) { - level := levels.Levels[levels.LevelInfo] - - event = &Event{ - logger: DefaultLogger, - level: level, - metadata: make(map[string]string), - } - - return -} - -func Warning() (event *Event) { - level := levels.Levels[levels.LevelWarning] - - event = &Event{ - logger: DefaultLogger, - level: level, - metadata: make(map[string]string), - } - - return -} - -func Error() (event *Event) { - level := levels.Levels[levels.LevelError] - - event = &Event{ - logger: DefaultLogger, - level: level, - metadata: make(map[string]string), - } - - return -} - -func Fatal() (event *Event) { - level := levels.Levels[levels.LevelFatal] - - event = &Event{ - logger: DefaultLogger, - level: level, - metadata: make(map[string]string), - } - - return -} diff --git a/log/streamer.go b/log/streamer.go deleted file mode 100644 index d56c7ad..0000000 --- a/log/streamer.go +++ /dev/null @@ -1,145 +0,0 @@ -package log - -import ( - "bytes" - "io" - "os" - "strings" -) - -type Streamer struct { - Logger *Logger - buf *bytes.Buffer - // If prefix == stdout, colors green - // If prefix == stderr, colors red - // Else, prefix is taken as-is, and prepended to anything - // you throw at Write() - prefix string - // if true, saves output in memory - record bool - persist string - - // Adds color to stdout & stderr if terminal supports it - colorOkay string - colorFail string - colorReset string -} - -// func NewLogstreamerForWriter(prefix string, writer io.Writer) *Streamer { -// logger := log.New(writer, prefix, 0) -// return NewLogstreamer(logger, "", false) -// } - -// func NewLogstreamerForStdout(prefix string) *Streamer { -// // logger := log.New(os.Stdout, prefix, log.Ldate|log.Ltime) -// logger := log.New(os.Stdout, prefix, 0) -// return NewLogstreamer(logger, "", false) -// } - -// func NewLogstreamerForStderr(prefix string) *Streamer { -// logger := log.New(os.Stderr, prefix, 0) -// return NewLogstreamer(logger, "", false) -// } - -func NewLogstreamer(logger *Logger, prefix string, record bool) *Streamer { - streamer := &Streamer{ - Logger: logger, - buf: bytes.NewBuffer([]byte("")), - prefix: prefix, - record: record, - persist: "", - colorOkay: "", - colorFail: "", - colorReset: "", - } - - if strings.HasPrefix(os.Getenv("TERM"), "xterm") { - streamer.colorOkay = "\x1b[32m" - streamer.colorFail = "\x1b[31m" - streamer.colorReset = "\x1b[0m" - } - - return streamer -} - -func (l *Streamer) Write(p []byte) (n int, err error) { - if n, err = l.buf.Write(p); err != nil { - return - } - - err = l.OutputLines() - return -} - -func (l *Streamer) Close() error { - if err := l.Flush(); err != nil { - return err - } - l.buf = bytes.NewBuffer([]byte("")) - return nil -} - -func (l *Streamer) Flush() error { - p := make([]byte, l.buf.Len()) - if _, err := l.buf.Read(p); err != nil { - return err - } - - l.out(string(p)) - return nil -} - -func (l *Streamer) OutputLines() error { - for { - line, err := l.buf.ReadString('\n') - - if len(line) > 0 { - if strings.HasSuffix(line, "\n") { - l.out(line) - } else { - // put back into buffer, it's not a complete line yet - // Close() or Flush() have to be used to flush out - // the last remaining line if it does not end with a newline - if _, err := l.buf.WriteString(line); err != nil { - return err - } - } - } - - if err == io.EOF { - break - } - - if err != nil { - return err - } - } - - return nil -} - -func (l *Streamer) FlushRecord() string { - buffer := l.persist - l.persist = "" - return buffer -} - -func (l *Streamer) out(str string) { - if len(str) < 1 { - return - } - - if l.record == true { - l.persist = l.persist + str - } - - if l.prefix == "stdout" { - str = l.colorOkay + l.prefix + l.colorReset + " " + str - } else if l.prefix == "stderr" { - str = l.colorFail + l.prefix + l.colorReset + " " + str - } else { - str = l.prefix + str - } - - l.Logger.Print().Msg(str) -} diff --git a/log/utils.go b/log/utils.go deleted file mode 100644 index 27df7a4..0000000 --- a/log/utils.go +++ /dev/null @@ -1,18 +0,0 @@ -package log - -import ( - "strings" - - "golang.org/x/term" -) - -func appendRest(data []byte, character string) []byte { - dataStr := string(data) - dataLen := len(dataStr) - - width, _, _ := term.GetSize(0) - - dataStr = dataStr + strings.Repeat(character[0:1], width-dataLen) - - return []byte(dataStr) -} diff --git a/log/writer/cli.go b/log/writer/cli.go deleted file mode 100644 index f380c32..0000000 --- a/log/writer/cli.go +++ /dev/null @@ -1,35 +0,0 @@ -package writer - -import ( - "os" - "sync" - - "github.com/hueristiq/hqgoutils/log/levels" -) - -type CLI struct{} - -var ( - _ Writer = &CLI{} - mutex *sync.Mutex -) - -func NewCLI() *CLI { - mutex = &sync.Mutex{} - - return &CLI{} -} - -func (w *CLI) Write(data []byte, level levels.LevelInt) { - mutex.Lock() - defer mutex.Unlock() - - switch level { - case levels.Levels[levels.LevelError], levels.Levels[levels.LevelFatal]: - os.Stderr.Write(data) - os.Stderr.Write([]byte("\n")) - default: - os.Stdout.Write(data) - os.Stdout.Write([]byte("\n")) - } -} diff --git a/log/writer/writer.go b/log/writer/writer.go deleted file mode 100644 index b7286d6..0000000 --- a/log/writer/writer.go +++ /dev/null @@ -1,7 +0,0 @@ -package writer - -import "github.com/hueristiq/hqgoutils/log/levels" - -type Writer interface { - Write(data []byte, level levels.LevelInt) -} diff --git a/ratelimiter/README.md b/ratelimiter/README.md deleted file mode 100644 index 51bb0fc..0000000 --- a/ratelimiter/README.md +++ /dev/null @@ -1,40 +0,0 @@ -# hqgoutils/ratelimiter - -A [Go(Golang)](https://golang.org/) package for handling rate limiting. - -## Resources - -* [Features](#features) -* [Usage](#usage) - -## Installation - -``` -go get -v -u github.com/hueristiq/hqgoutils/ratelimiter -``` - -## Usage - -```go -package main - -import ( - "fmt" - "github.com/hueristiq/hqgoutils/ratelimiter" -) - -func main() { - options := &ratelimiter.Options{ - RequestsPerMinute: 40, - MinimumDelayInSeconds: 2, - } - - limiter := ratelimiter.New(options) - - // Make 10 requests and ensure that they are rate limited. - for i := 1; i <= 10; i++ { - limiter.Wait() - fmt.Printf("Request %d made at %v\n", i, time.Now()) - } -} -``` \ No newline at end of file diff --git a/ratelimiter/doc.go b/ratelimiter/doc.go deleted file mode 100644 index cee70a3..0000000 --- a/ratelimiter/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package ratelimiter provides an implementation of client rate limiting in Go. -package ratelimiter diff --git a/ratelimiter/ratelimiter.go b/ratelimiter/ratelimiter.go deleted file mode 100644 index 923f4a5..0000000 --- a/ratelimiter/ratelimiter.go +++ /dev/null @@ -1,61 +0,0 @@ -package ratelimiter - -import ( - "sync" - "time" -) - -// RateLimiter implements rate limiting to limit the number of requests made within a certain time period. -type RateLimiter struct { - requestsPerMinute int - minimumDelayInSeconds int - timeOfLastRequest time.Time - lock sync.Mutex -} - -// Options implements the structure of RateLimiter creation options. -type Options struct { - RequestsPerMinute int - MinimumDelayInSeconds int -} - -// New creates a new *RateLimiter with the specified *Options. -func New(options *Options) (limiter *RateLimiter) { - limiter = &RateLimiter{ - requestsPerMinute: options.RequestsPerMinute, - minimumDelayInSeconds: options.MinimumDelayInSeconds, - timeOfLastRequest: time.Now(), - } - - return -} - -// Wait waits until the next request can be made within the rate limit. -func (limiter *RateLimiter) Wait() { - limiter.lock.Lock() - defer limiter.lock.Unlock() - - var timeToSleep time.Duration - - // calculate the time that has elapsed since the last request was made, based on the timeOfLastRequest value of the limiter object. - timeSinceLastRequest := time.Since(limiter.timeOfLastRequest) - - if limiter.requestsPerMinute > 0 { - // calculate the minimum interval (in time.Duration units) that should be enforced between requests in order to comply with the rate limiting policy. - interval := time.Duration(time.Minute.Nanoseconds() / int64(limiter.requestsPerMinute)) - // calculate the amount of time that the program needs to sleep before making another request, in order to comply with the rate limiting policy. - timeToSleep = interval - timeSinceLastRequest - } - - minimumDelayInNanoseconds := time.Duration(limiter.minimumDelayInSeconds) * time.Second - - if timeSinceLastRequest < minimumDelayInNanoseconds { - timeToSleep = minimumDelayInNanoseconds - timeSinceLastRequest - } - - // sleep - time.Sleep(timeToSleep) - - // Update the last access time to the current time. - limiter.timeOfLastRequest = time.Now() -} diff --git a/ssh/README.md b/ssh/README.md deleted file mode 100644 index 793ee42..0000000 --- a/ssh/README.md +++ /dev/null @@ -1,142 +0,0 @@ -# hqgoutils/ssh - -A [Go(Golang)](https://golang.org/) package to provide a simple abstraction around [ssh](https://pkg.go.dev/golang.org/x/crypto/ssh) and [sftp](https://pkg.go.dev/github.com/pkg/sftp) packages. - -## Resources - -* [Features](#features) -* [Usage](#usage) - * [Authentication with password](#authentication-with-password) - * [Authentication with key with passphrase](#authentication-with-key-with-passphrase) - * [Authentication with Key without passphrase](#authentication-with-key-without-passphrase) - * [Run remote commands](#run-remote-commands) - * [Attach interactive shell](#attach-interactive-shell) - * [Files upload and download](#files-upload-and-download) - * [Upload Local File/Directory to Remote](#upload-local-filedirectory-to-remote) - * [Download Remote File/Directory to Local](#download-remote-filedirectory-to-local) -* [Contribution](#contribution) - -## Features - -- [x] Authentication with password. -- [x] Authentication with keys with passphrase. -- [x] Authentication with keys without passphrase. -- [x] Supports running remote commands. -- [x] Supports getting an interactive shell. -- [x] Supports files upload and download. - -## Installation - -``` -go get -v -u github.com/hueristiq/hqgoutils/ssh -``` -## Usage - -### Authentication with password - -```go -auth, err := authentication.Password("Password") -if err != nil { - log.Fatal(err) -} - -client, err := ssh.New(&ssh.Configuration{ - Host: "xxx.xxx.xxx.xxx", - Port: 22, - User: "some-user", - Authentication: auth, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), -}) -if err != nil { - log.Println(err) -} - -defer client.Close() -``` - -### Authentication with key with passphrase - -```go -auth, err := authentication.KeyWithPassphrase(privateKey, "Passphrase") -if err != nil { - log.Fatal(err) -} - -client, err := ssh.New(&ssh.Configuration{ - Host: "xxx.xxx.xxx.xxx", - Port: 22, - User: "some-user", - Authentication: auth, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), -}) -if err != nil { - log.Println(err) -} - -defer client.Close() -``` - -### Authentication with key without passphrase - -```go -auth, err := authentication.Key(privateKey) -if err != nil { - log.Fatal(err) -} - -client, err := ssh.New(&ssh.Configuration{ - Host: "xxx.xxx.xxx.xxx", - Port: 22, - User: "some-user", - Authentication: auth, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), -}) -if err != nil { - log.Println(err) -} - -defer client.Close() -``` - -### Run remote commands - -```go -if err = client.Run(&ssh.Command{ - CMD: "echo ${LC_TEST}", - ENV: map[string]string{"LC_TEST":"working"}, - Stdin: os.Stdin, - Stdout: os.Stdout, - Stderr: os.Stderr, -}); err != nil { - log.Fatal(err) -} -``` - -### Attach interactive shell - -```go -if err = client.Shell(); err != nil { - log.Println(err) -} -``` - -### Files upload and download -#### Upload Local File/Directory to Remote - -```go -if err := client.Upload("/path/to/local/file", "/path/to/remote/file"); err != nil { - log.Println(err) -} -``` - -#### Download Remote File/Directory to Local - -```go -if err := client.Download("/path/to/remote/file", "/path/to/local/file"); err != nil { - log.Println(err) -} -``` - -## Contribution - -[Issues](https://github.com/hueristiq/ssh/issues) and [Pull Requests](https://github.com/hueristiq/ssh/pulls) are welcome! \ No newline at end of file diff --git a/ssh/authentication/authentication.go b/ssh/authentication/authentication.go deleted file mode 100644 index be4fdb1..0000000 --- a/ssh/authentication/authentication.go +++ /dev/null @@ -1,45 +0,0 @@ -package authentication - -import ( - "golang.org/x/crypto/ssh" -) - -type Authentication []ssh.AuthMethod - -func Password(password string) (authentication Authentication) { - authentication = Authentication{ - ssh.Password(password), - } - - return -} - -func KeyWithPassphrase(privateKey, passphrase string) (authentication Authentication, err error) { - var signer ssh.Signer - - signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(privateKey), []byte(passphrase)) - if err != nil { - return - } - - authentication = Authentication{ - ssh.PublicKeys(signer), - } - - return -} - -func KeyWithoutPassphrase(privateKey string) (authentication Authentication, err error) { - var signer ssh.Signer - - signer, err = ssh.ParsePrivateKey([]byte(privateKey)) - if err != nil { - return - } - - authentication = Authentication{ - ssh.PublicKeys(signer), - } - - return -} diff --git a/ssh/command.go b/ssh/command.go deleted file mode 100644 index 7f7cd23..0000000 --- a/ssh/command.go +++ /dev/null @@ -1,12 +0,0 @@ -package ssh - -import "io" - -// Command represents remote commands structure. -type Command struct { - CMD string - ENV map[string]string - Stdin io.Reader - Stdout io.Writer - Stderr io.Writer -} diff --git a/ssh/keys/keys.go b/ssh/keys/keys.go deleted file mode 100644 index d1589f3..0000000 --- a/ssh/keys/keys.go +++ /dev/null @@ -1,147 +0,0 @@ -package keys - -import ( - "bytes" - "crypto/rand" - "crypto/rsa" - "crypto/x509" - "encoding/pem" - "fmt" - "io/ioutil" - "os" - "path/filepath" - - "golang.org/x/crypto/ssh" -) - -func Read(path string) (priv string, pub string, err error) { - var ( - stats os.FileInfo - privBuf, pubBUf []byte - ) - - stats, err = os.Stat(path) - if err != nil { - err = fmt.Errorf("failed reading private key: %s", err) - - return - } - - if stats.IsDir() { - err = fmt.Errorf("failed reading private key: %s is a directory!", path) - - return - } - - privBuf, err = ioutil.ReadFile(path) - if err != nil { - err = fmt.Errorf("failed reading private key: %s", err) - - return - } - - priv = string(privBuf) - - pubBUf, err = ioutil.ReadFile(path + ".pub") - if err != nil { - err = fmt.Errorf("failed reading public key: %s", err) - - return - } - - pub = string(pubBUf) - - return -} - -func Generate() (priv string, pub string, err error) { - var ( - privateKey *rsa.PrivateKey - publicKey ssh.PublicKey - privBuf bytes.Buffer - pubBUf []byte - ) - - privateKey, err = rsa.GenerateKey(rand.Reader, 2048) - if err != nil { - err = fmt.Errorf("failed generating private key: %s", err) - - return - } - - privateKeyPEM := &pem.Block{ - Type: "RSA PRIVATE KEY", - Bytes: x509.MarshalPKCS1PrivateKey(privateKey), - } - - if err = pem.Encode(&privBuf, privateKeyPEM); err != nil { - err = fmt.Errorf("failed encoding private key: %s", err) - - return - } - - priv = privBuf.String() - - publicKey, err = ssh.NewPublicKey(&privateKey.PublicKey) - if err != nil { - err = fmt.Errorf("failed generating public key: %s", err) - - return - } - - pubBUf = ssh.MarshalAuthorizedKey(publicKey) - pub = string(pubBUf) - - return -} - -func ReadOrGenerate(path string) (priv string, pub string, err error) { - pub, priv, err = Read(path) - if err != nil { - goto GENERATE - } else { - return - } - -GENERATE: - pub, priv, err = Generate() - if err != nil { - err = fmt.Errorf("failed generating keys: %s", err) - - return - } - - if err = Write(path, pub, priv); err != nil { - err = fmt.Errorf("failed writing keys: %s", err) - - return - } - - return -} - -func Write(path, pub, priv string) (err error) { - directory := filepath.Dir(path) - - if _, err = os.Stat(directory); err != nil { - if os.IsNotExist(err) { - if directory != "" { - if err = os.MkdirAll(directory, os.ModePerm); err != nil { - return - } - } - } else { - return - } - } - - if err = ioutil.WriteFile(path, []byte(priv), 0600); err != nil { - return - } - - if err = ioutil.WriteFile(path+".pub", []byte(pub), 0644); err != nil { - return - } - - return -} diff --git a/ssh/sftp.go b/ssh/sftp.go deleted file mode 100644 index a21bb30..0000000 --- a/ssh/sftp.go +++ /dev/null @@ -1,236 +0,0 @@ -package ssh - -import ( - "errors" - "io" - "os" - "path/filepath" - "strings" -) - -var ( - confirmSourceType = true - - ErrSourceNotFound = errors.New("source not found") - ErrSourceIsDirectory = errors.New("source is directory") - ErrSourceIsFile = errors.New("source is file") -) - -// Upload transfers local directories and files to remote host. -// This works by calling UploadDirectory or UploadFile depending on the source. -func (client *Client) Upload(SRC, DEST string) (err error) { - confirmSourceType = false - - stat, err := os.Stat(SRC) - if os.IsNotExist(err) { - return ErrSourceNotFound - } else if err != nil && !os.IsNotExist(err) { - return - } - - if stat.IsDir() { - if err = client.UploadDirectory(SRC, DEST); err != nil { - return - } - } else { - if err = client.UploadFile(SRC, DEST); err != nil { - return - } - } - - return -} - -// UploadDirectory transfers local directories and contained files recursively to remote host -func (client *Client) UploadDirectory(SRC, DEST string) (err error) { - if confirmSourceType { - var stat os.FileInfo - - stat, err = os.Stat(SRC) - if os.IsNotExist(err) { - return ErrSourceNotFound - } else if err != nil && !os.IsNotExist(err) { - return - } - - if !stat.IsDir() { - return ErrSourceIsFile - } - } - - if err = filepath.Walk(SRC, func(fileSRC string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if !info.IsDir() { - fileDEST := filepath.Join(DEST, strings.TrimPrefix(fileSRC, SRC)) - - if err := client.UploadFile(fileSRC, fileDEST); err != nil { - return err - } - } - - return nil - }); err != nil { - return - } - - return -} - -// UploadFile transfers local files to remote host -func (client *Client) UploadFile(SRC, DEST string) (err error) { - if confirmSourceType { - var stat os.FileInfo - - stat, err = os.Stat(SRC) - if os.IsNotExist(err) { - return ErrSourceNotFound - } else if err != nil && !os.IsNotExist(err) { - return - } - - if stat.IsDir() { - return ErrSourceIsDirectory - } - } - - directory := filepath.Dir(DEST) - - _, err = client.SFTP.Stat(directory) - if os.IsNotExist(err) { - if err = client.SFTP.MkdirAll(directory); err != nil { - return - } - } else if err != nil && !os.IsNotExist(err) { - return - } - - SRCFile, err := os.Open(SRC) - if err != nil { - return - } - - defer SRCFile.Close() - - DESTFile, err := client.SFTP.OpenFile(DEST, (os.O_WRONLY | os.O_CREATE | os.O_TRUNC)) - if err != nil { - return - } - - defer DESTFile.Close() - - if _, err = io.Copy(DESTFile, SRCFile); err != nil { - return - } - - return -} - -// Download transfers remote directories and files to local host. -// This works by calling DownloadDirectory or DownloadFile depending on the source. -func (client *Client) Download(SRC, DEST string) (err error) { - confirmSourceType = false - - stat, err := client.SFTP.Stat(SRC) - if os.IsNotExist(err) { - return ErrSourceNotFound - } else if err != nil && !os.IsNotExist(err) { - return - } - - if stat.IsDir() { - if err = client.DownloadDirectory(SRC, DEST); err != nil { - return - } - } else { - if err = client.DownloadFile(SRC, DEST); err != nil { - return - } - } - - return -} - -// DownloadDirectory transfers remote directories and contained files recursively to local host -func (client *Client) DownloadDirectory(SRC, DEST string) (err error) { - if confirmSourceType { - var stat os.FileInfo - - stat, err = client.SFTP.Stat(SRC) - if os.IsNotExist(err) { - return ErrSourceNotFound - } else if err != nil && !os.IsNotExist(err) { - return - } - - if !stat.IsDir() { - return ErrSourceIsFile - } - } - - walker := client.SFTP.Walk(SRC) - - for walker.Step() { - if err = walker.Err(); err != nil { - return - } - - if !walker.Stat().IsDir() { - fileDEST := filepath.Join(DEST, strings.TrimPrefix(walker.Path(), SRC)) - - if err = client.DownloadFile(walker.Path(), fileDEST); err != nil { - return - } - } - } - - return -} - -// DownloadFile transfers remote files to local host -func (client *Client) DownloadFile(SRC, DEST string) (err error) { - if confirmSourceType { - var stat os.FileInfo - - stat, err = client.SFTP.Stat(SRC) - if os.IsNotExist(err) { - return ErrSourceNotFound - } else if err != nil && !os.IsNotExist(err) { - return - } - - if stat.IsDir() { - return ErrSourceIsDirectory - } - } - - directory := filepath.Dir(DEST) - - if _, err = os.Stat(directory); os.IsNotExist(err) { - if err = os.MkdirAll(directory, os.ModePerm); err != nil { - return - } - } - - SRCFile, err := client.SFTP.OpenFile(SRC, (os.O_RDONLY)) - if err != nil { - return - } - - defer SRCFile.Close() - - DESTFile, err := os.Create(DEST) - if err != nil { - return - } - - defer DESTFile.Close() - - if _, err = io.Copy(DESTFile, SRCFile); err != nil { - return - } - - return -} diff --git a/ssh/ssh.go b/ssh/ssh.go deleted file mode 100644 index 0d63331..0000000 --- a/ssh/ssh.go +++ /dev/null @@ -1,235 +0,0 @@ -package ssh - -import ( - "fmt" - "io" - "net" - "os" - "time" - - "github.com/hueristiq/hqgoutils/ssh/authentication" - "github.com/pkg/sftp" - "golang.org/x/crypto/ssh" - "golang.org/x/crypto/ssh/terminal" -) - -// Client represents a client consisting of an SSH client and an SFTP session -type Client struct { - SSH *ssh.Client - SFTP *sftp.Client -} - -// Options represents options used in creating a Client -type Options struct { - Host string - Port int - User string - Authentication authentication.Authentication - Timeout int - HostKeyCallback ssh.HostKeyCallback -} - -const ( - DefaultTimeout = 30 -) - -// New create Client -func New(options *Options) (client *Client, err error) { - client = &Client{ - SSH: &ssh.Client{}, - SFTP: &sftp.Client{}, - } - - if options.Timeout <= 0 { - options.Timeout = DefaultTimeout - } - - server := net.JoinHostPort(options.Host, fmt.Sprint(options.Port)) - config := &ssh.ClientConfig{ - User: options.User, - Auth: options.Authentication, - Timeout: time.Duration(options.Timeout) * time.Second, - HostKeyCallback: options.HostKeyCallback, - } - - retry, retryDelay, maxRetries := 1, 5, 10 - -CREATE_CLIENT: - if client.SSH, err = ssh.Dial("tcp", server, config); err != nil { - if retry <= maxRetries { - time.Sleep(time.Duration(retryDelay) * time.Second) - - retry++ - - goto CREATE_CLIENT - } - - return - } - - retry = 1 - - if client.SFTP, err = sftp.NewClient(client.SSH); err != nil { - if retry <= maxRetries { - time.Sleep(time.Duration(retryDelay) * time.Second) - - retry++ - - goto CREATE_CLIENT - } - - return - } - - return -} - -// Run runs cmd on the remote host. -func (client *Client) Run(command *Command) (err error) { - var ( - session *ssh.Session - stdin io.WriteCloser - stdout, stderr io.Reader - ) - - session, err = client.SSH.NewSession() - if err != nil { - return - } - - defer session.Close() - - if command.Stdin != nil { - stdin, err = session.StdinPipe() - if err != nil { - return - } - - go io.Copy(stdin, command.Stdin) - } - - if command.Stdout != nil { - stdout, err = session.StdoutPipe() - if err != nil { - return - } - - go io.Copy(command.Stdout, stdout) - } - - if command.Stderr != nil { - stderr, err = session.StderrPipe() - if err != nil { - return - } - - go io.Copy(command.Stderr, stderr) - } - - for variable, value := range command.ENV { - if err = session.Setenv(variable, value); err != nil { - return - } - } - - term := os.Getenv("TERM") - if term == "" { - term = "xterm-256color" - } - - termmodes := ssh.TerminalModes{ - ssh.ECHO: 0, // disable echoing - ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud - ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud - ssh.OPOST: 1, // Enable output processing. - } - - if err = session.RequestPty(term, 40, 80, termmodes); err != nil { - return - } - - if err = session.Run(command.CMD); err != nil { - return - } - - return -} - -// Shell starts a login shell on the remote host. -func (client *Client) Shell() (err error) { - var ( - session *ssh.Session - ) - - session, err = client.SSH.NewSession() - if err != nil { - return - } - - defer session.Close() - - session.Stdin = os.Stdin - session.Stdout = os.Stdout - session.Stderr = os.Stderr - - term := os.Getenv("TERM") - if term == "" { - term = "xterm-256color" - } - - termmodes := ssh.TerminalModes{ - ssh.ECHO: 0, // disable echoing - ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud - ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud - ssh.OPOST: 1, // Enable output processing. - } - - fd := int(os.Stdin.Fd()) - - state, err := terminal.MakeRaw(fd) - if err != nil { - return - } - - defer terminal.Restore(fd, state) - - width, height, err := terminal.GetSize(fd) - if err != nil { - return - } - - if err = session.RequestPty(term, height, width, termmodes); err != nil { - return - } - - if err = session.Shell(); err != nil { - return - } - - if err = session.Wait(); err != nil { - return - } - - return -} - -// Close closes SFTP session and the underlying network connection -func (client *Client) Close() (err error) { - if client == nil { - return - } - - if client.SFTP != nil { - if err = client.SFTP.Close(); err != nil { - return - } - } - - if client.SSH != nil { - if err = client.SSH.Close(); err != nil { - return - } - } - - return -} diff --git a/url/README.md b/url/README.md deleted file mode 100644 index 2f33759..0000000 --- a/url/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# hqgoutils/url - -A [Golang](http://golang.org/) package for handling URLs. - -## Installation - -``` -go get -v -u github.com/hueristiq/hqgoutils/url -``` \ No newline at end of file diff --git a/url/url.go b/url/url.go deleted file mode 100644 index 27db471..0000000 --- a/url/url.go +++ /dev/null @@ -1,117 +0,0 @@ -package url - -import ( - "fmt" - "net/url" - "path" - "strings" - - "golang.org/x/net/publicsuffix" -) - -// URL represents a parsed URL (technically, a URI reference). -// -// The general form represented is: -// -// [scheme:][//[userinfo@]host][/]path[?query][#fragment] -// -// URLs that do not start with a slash after the scheme are interpreted as: -// -// scheme:opaque[?query][#fragment] -// -// https://sub.example.com:8080/path/to/file.txt -type URL struct { - *url.URL - // Scheme string // e.g https - // Opaque string // encoded opaque data - // User *Userinfo // username and password information - // Host string // e.g. sub.example.com, sub.example.com:8080 - // Path string // path (relative paths may omit leading slash) e.g /path/to/file.txt - // RawPath string // encoded path hint (see EscapedPath method) - // OmitHost bool // do not emit empty host (authority) - // ForceQuery bool // append a query ('?') even if RawQuery is empty - // RawQuery string // encoded query values, without '?' - // Fragment string // fragment for references, without '#' - // RawFragment string // encoded fragment hint (see EscapedFragment method) - Domain string // e.g. sub.example.com - ETLDPlusOne string // e.g. example.com - Subdomain string // e.g. sub - RootDomain string // e.g. example - TLD string // e.g. com - Port string // e.g. 8080 - Extension string // e.g. txt -} - -// Parse parses a raw url into a URL structure. -// -// It uses the `net/url`'s Parse() internally, but it slightly changes its behavior: -// 1. It forces the default scheme, if the url doesnt have a scheme, to http -// 2. It favors absolute paths over relative ones, thus "example.com" -// is parsed into url.Host instead of url.Path. -// 3. It lowercases the Host (not only the Scheme). -func Parse(rawURL string) (parsedURL *URL, err error) { - const defaultScheme string = "http" - - rawURL = AddDefaultScheme(rawURL, defaultScheme) - - parsedURL = &URL{} - - parsedURL.URL, err = url.Parse(rawURL) - if err != nil { - err = fmt.Errorf("[hqgoutils/url]: %w", err) - - return - } - - // Host = Domain + Port - parsedURL.Domain, parsedURL.Port = SplitHost(parsedURL.URL.Host) - - // ETLDPlusOne - parsedURL.ETLDPlusOne, err = publicsuffix.EffectiveTLDPlusOne(parsedURL.Domain) - if err != nil { - err = fmt.Errorf("[hqgoutils/url] %w", err) - - return - } - - // RootDomain + TLD - i := strings.Index(parsedURL.ETLDPlusOne, ".") - parsedURL.RootDomain = parsedURL.ETLDPlusOne[0:i] - parsedURL.TLD = parsedURL.ETLDPlusOne[i+1:] - - // Subdomain - if rest := strings.TrimSuffix(parsedURL.Domain, "."+parsedURL.ETLDPlusOne); rest != parsedURL.Domain { - parsedURL.Subdomain = rest - } - - // Extension - parsedURL.Extension = path.Ext(parsedURL.Path) - - return -} - -// AddDefaultScheme ensures a scheme is added if none exists. -func AddDefaultScheme(rawURL, scheme string) string { - switch { - case strings.HasPrefix(rawURL, "//"): - return scheme + ":" + rawURL - case strings.Contains(rawURL, "://") && !strings.HasPrefix(rawURL, "http"): - return scheme + rawURL - case !strings.Contains(rawURL, "://"): - return scheme + "://" + rawURL - default: - return rawURL - } -} - -// splitHost splits the host into domain and port. -func SplitHost(host string) (domain string, port string) { - for i := len(host) - 1; i >= 0; i-- { - if host[i] == ':' { - return host[:i], host[i+1:] - } else if host[i] < '0' || host[i] > '9' { - domain = host - } - } - return -} diff --git a/url/url_test.go b/url/url_test.go deleted file mode 100644 index 4267aef..0000000 --- a/url/url_test.go +++ /dev/null @@ -1,142 +0,0 @@ -package url_test - -import ( - "testing" - - "github.com/hueristiq/hqgoutils/url" -) - -func TestParse(t *testing.T) { - tests := []struct { - name string - input string - output *url.URL - err error - }{ - { - name: "Test example URL", - input: "https://sub.example.com:8080/path/to/file.txt", - output: &url.URL{ - Domain: "sub.example.com", - ETLDPlusOne: "example.com", - Subdomain: "sub", - RootDomain: "example", - TLD: "com", - Port: "8080", - Extension: ".txt", - }, - err: nil, - }, - } - - for index := range tests { - tt := tests[index] - - t.Run(tt.name, func(t *testing.T) { - got, err := url.Parse(tt.input) - if err != nil { - t.Errorf("Parse(%q) returned error %v", tt.input, err) - } - - if got.Domain != tt.output.Domain || got.ETLDPlusOne != tt.output.ETLDPlusOne || got.Subdomain != tt.output.Subdomain || got.RootDomain != tt.output.RootDomain || got.TLD != tt.output.TLD || got.Port != tt.output.Port || got.Extension != tt.output.Extension { - t.Errorf("Parse(%q) = %v, want %v", tt.input, got, tt.output) - } - }) - } -} - -func TestAddDefaultScheme(t *testing.T) { - tests := []struct { - name string - url string - scheme string - output string - }{ - { - name: "Case: localhost", - url: "localhost", - scheme: "http", - output: "http://localhost", - }, - { - name: "Case: example.com", - url: "example.com", - scheme: "http", - output: "http://example.com", - }, - { - name: "Case: //example.com", - url: "//example.com", - scheme: "http", - output: "http://example.com", - }, - { - name: "Case: ://example.com", - url: "://example.com", - scheme: "http", - output: "http://example.com", - }, - { - name: "Case: https://example.com", - url: "https://example.com", - scheme: "http", - output: "https://example.com", - }, - } - - for index := range tests { - tt := tests[index] - - t.Run(tt.name, func(t *testing.T) { - got := url.AddDefaultScheme(tt.url, tt.scheme) - if got != tt.output { - t.Errorf("AddDefaultScheme(%q, %q) = %v, want %v", tt.url, tt.scheme, got, tt.output) - } - }) - } -} - -func TestSplitHost(t *testing.T) { - tests := []struct { - name string - host string - domain string - port string - }{ - { - name: "Case: localhost", - host: "localhost", - domain: "localhost", - port: "", - }, - { - name: "Case: example.com", - host: "example.com", - domain: "example.com", - port: "", - }, - { - name: "Case: localhost:8080", - host: "localhost:8080", - domain: "localhost", - port: "8080", - }, - { - name: "Case: example.com:8080", - host: "example.com:8080", - domain: "example.com", - port: "8080", - }, - } - - for index := range tests { - tt := tests[index] - - t.Run(tt.name, func(t *testing.T) { - domain, port := url.SplitHost(tt.host) - if domain != tt.domain || port != tt.port { - t.Errorf("splitHost(%q) = %v, %v, want %v, %v", tt.host, domain, port, tt.domain, tt.port) - } - }) - } -}