diff --git a/.github/.goreleaser.yml b/.github/.goreleaser.yml index bed8c71424..866e03f783 100644 --- a/.github/.goreleaser.yml +++ b/.github/.goreleaser.yml @@ -1,56 +1,50 @@ -project_name: kanvas-snapshot +project_name: helm-kanvas-snapshot version: 2 before: hooks: - go mod tidy builds: - - main: cmd/kanvas-snapshot/main.go - - env: + - env: - CGO_ENABLED=0 - ldflags: - -s -w - - -X main.providerToken={{.Env.PROVIDER_TOKEN}} + - -X main.providerToken={{.Env.PROVIDER_TOKEN}} - -X main.MesheryCloudApiBaseUrl="https://meshery.layer5.io" - -X main.MesheryApiBaseUrl="https://playground.meshery.io" - goos: - - darwin - linux + - darwin + - freebsd - windows - goarch: - - 386 - amd64 + - '386' - arm - arm64 - - ignore: - - goos: windows - goarch: arm - - goos: windows - goarch: arm64 + binary: '{{ .ProjectName }}' archives: - - id: stable + - format: tar.gz + wrap_in_directory: true name_template: >- - {{ .ProjectName }}_{{.Version}}_ - {{- title .Os }}_ + {{- .ProjectName }}_ + {{- .Version }}_ + {{- .Os }}_ {{- if eq .Arch "amd64" }}x86_64 {{- else if eq .Arch "386" }}i386 {{- else }}{{ .Arch }}{{ end }} - format: tar.gz - format_overrides: - - goos: windows - format: zip - -checksum: - name_template: 'checksums.txt' + files: + - plugin.yaml + - LICENSE + - install-binary.sh snapshot: - version_template: "{{ .Tag }}-next" + name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Runtime.Goos }}_{{ .Runtime.Goarch }}" + +checksum: + name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS' + algorithm: sha256 changelog: sort: asc diff --git a/.github/workflows/build-and-release-snapshot-plugin.yml b/.github/workflows/build-and-release-snapshot-plugin.yml index e43de11f6c..79a3d0efd5 100644 --- a/.github/workflows/build-and-release-snapshot-plugin.yml +++ b/.github/workflows/build-and-release-snapshot-plugin.yml @@ -5,6 +5,11 @@ on: types: [published] workflow_dispatch: + + pull_request: # Add this to trigger on PR events + branches: + - install-script-enhancement + jobs: release: if: github.repository == 'meshery/helm-kanvas-snapshot' && startsWith(github.ref, 'refs/tags/v') && !contains(github.ref, 'patch') && !contains(github.ref, 'alpha') && !contains(github.ref, 'beta') && !contains(github.ref, 'rc') @@ -20,21 +25,21 @@ jobs: uses: actions/setup-go@v5 with: go-version: "1.21" - - - name: golangci-lint + + - name: Run golangci-lint uses: golangci/golangci-lint-action@v6 with: version: v1.59 - working-directory: ./cmd/kanvas-snapshot + working-directory: . args: --timeout 10m --verbose skip-cache: true - name: Run GoReleaser with tag uses: goreleaser/goreleaser-action@v6 + with: + version: latest # Use latest GoReleaser action version + args: release --clean --skip-validate -f goreleaser.yml # Corrected filename to root env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} RELEASE_CHANNEL: "stable" PROVIDER_TOKEN: ${{ secrets.PROVIDER_TOKEN }} - with: - version: 2 - args: release --clean --skip validate -f .github/.goreleaser.yml diff --git a/Makefile b/Makefile index 9288ead354..289d5fa46f 100644 --- a/Makefile +++ b/Makefile @@ -12,12 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -include build/Makefile.core.mk +# include build/Makefile.core.mk .PHONY: all all: dep-check build -## Lint check +## Lint check golangci: error dep-check golangci-lint run --exclude-use-default @@ -29,11 +29,9 @@ error: dep-check error-util: go run github.com/layer5io/meshkit/cmd/errorutil -d . update -i ./build -o ./build - - -#----------------------------------------------------------------------------- -# Dependencies -#----------------------------------------------------------------------------- +# #----------------------------------------------------------------------------- +# # Dependencies +# #----------------------------------------------------------------------------- .PHONY: dep-check #.SILENT: dep-check @@ -48,47 +46,66 @@ ifeq (,$(findstring $(GOVERSION), $(INSTALLED_GO_VERSION))) @echo "INSTALLED_GO_VERSION: " $(INSTALLED_GO_VERSION) endif +GOFMT_FILES?=$(shell find . -type f -name '*.go' -not -path "./vendor/*") +APP_NAME?=helm-kanvas-snapshot +APP_DIR?=$(shell git rev-parse --show-toplevel) +DEV?=${DEVBOX_TRUE} +SRC_PACKAGES=$(shell go list ./... | grep -v "mocks") +BUILD_ENVIRONMENT?=${ENVIRONMENT} +VERSION?=0.1.0 +REVISION?=$(shell git rev-parse --verify HEAD) +DATE?=$(shell date) +PLATFORM?=$(shell go env GOOS) +ARCHITECTURE?=$(shell go env GOARCH) +GOVERSION?=$(shell go version | awk '{printf $$3}') +BUILD_WITH_FLAGS="-s -w -X 'github.com/meshery/helm-kanvas-snapshot/version.Version=${VERSION}' -X 'github.com/meshery/helm-kanvas-snapshot/version.Env=${BUILD_ENVIRONMENT}' -X 'github.com/meshery/helm-kanvas-snapshot/version.BuildDate=${DATE}' -X 'github.com/meshery/helm-kanvas-snapshot/version.Revision=${REVISION}' -X 'github.com/meshery/helm-kanvas-snapshot/version.Platform=${PLATFORM}/${ARCHITECTURE}' -X 'github.com/meshery/helm-kanvas-snapshot/version.GoVersion=${GOVERSION}' -X 'main.providerToken=$(PROVIDER_TOKEN)' -X 'main.MesheryCloudApiBaseUrl=$(MESHERY_CLOUD_API_BASE_URL)' -X 'main.MesheryApiBaseUrl=$(MESHERY_API_BASE_URL)'" + +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) +ifeq (,$(shell go env GOBIN)) +GOBIN=$(shell go env GOPATH)/bin +else +GOBIN=$(shell go env GOBIN) +endif + +.PHONY: help +help: ## Prints help (only for targets with comments) + @grep -E '^[a-zA-Z0-9._-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +local.fmt: ## Lints all the Go code in the application. + @gofmt -w $(GOFMT_FILES) + $(GOBIN)/goimports -w $(GOFMT_FILES) + $(GOBIN)/gofumpt -l -w $(GOFMT_FILES) + $(GOBIN)/gci write $(GOFMT_FILES) --skip-generated + +local.check: local.fmt ## Loads all dependencies + @go mod tidy + +local.build: local.check ## Generates the artifact with 'go build' + @go build -o $(APP_NAME) -ldflags="-s -w" + +local.snapshot: local.check ## Generates the artifact with 'go build' + GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} goreleaser build --snapshot --clean + +local.push: local.build ## Pushes built artifact to specified location + +local.run: local.build ## Builds the artifact and starts the service + ./${APP_NAME} + +print_home: + @echo ${ENVIRONMENT} + + +local.deploy: local.build ## Deploys locally built Helm plugin + @rm -rf ${HOME}/Library/helm/plugins/helm-kanvas-snapshot/bin/helm-kanvas-snapshot + @cp helm-images ${HOME}/Library/helm/plugins/helm-kanvas-snapshot/bin/helm-kanvas-snapshot + + +publish: local.check ## Builds and publishes the app + GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} PLUGIN_PATH=${APP_DIR} goreleaser release --snapshot --clean + +mock.publish: local.check ## Builds and mocks app release + GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} PLUGIN_PATH=${APP_DIR} goreleaser release --skip=publish --clean + -OUTDIR := ./cmd/kanvas-snapshot/bin -ARCH := amd64 - -BINNAME_DARWIN ?= kanvas-snapshot-darwin-$(ARCH) -BINNAME_LINUX ?= kanvas-snapshot-linux-$(ARCH) -BINNAME_WINDOWS ?= kanvas-snapshot-windows-$(ARCH).exe - - -LDFLAGS := "\ - -X 'main.providerToken=$(PROVIDER_TOKEN)' \ - -X 'main.MesheryCloudApiBaseUrl=$(MESHERY_CLOUD_API_BASE_URL)' \ - -X 'main.MesheryApiBaseUrl=$(MESHERY_API_BASE_URL)'" - -.PHONY: build -build: - @echo "Building for all platforms..." - @$(MAKE) $(BINNAME_DARWIN) - @$(MAKE) $(BINNAME_LINUX) - @$(MAKE) $(BINNAME_WINDOWS) - -# Build Helm plugin for Darwin (macOS) -.PHONY: $(BINNAME_DARWIN) -$(BINNAME_DARWIN): - @echo "Building for Darwin..." - CGO_ENABLED=0 GOARCH=$(ARCH) GOOS=darwin go build -ldflags=$(LDFLAGS) -o $(OUTDIR)/$(BINNAME_DARWIN) ./cmd/kanvas-snapshot/main.go - -# Build Helm plugin for Linux -.PHONY: $(BINNAME_LINUX) -$(BINNAME_LINUX): - @echo "Building for Linux..." - CGO_ENABLED=0 GOARCH=$(ARCH) GOOS=linux go build -ldflags=$(LDFLAGS) -o $(OUTDIR)/$(BINNAME_LINUX) ./cmd/kanvas-snapshot/main.go - -# Build Helm plugin for Windows -.PHONY: $(BINNAME_WINDOWS) -$(BINNAME_WINDOWS): - @echo "Building for Windows..." - CGO_ENABLED=0 GOARCH=$(ARCH) GOOS=windows go build -ldflags=$(LDFLAGS) -o $(OUTDIR)/$(BINNAME_WINDOWS) ./cmd/kanvas-snapshot/main.go - -# Clean up binaries -.PHONY: clean -clean: - @echo "Cleaning up..." - rm -rf $(OUTDIR) +install.hooks: ## Installs pre-push hooks for the repository + ${APP_DIR}/scripts/hook.sh ${APP_DIR} diff --git a/artifacthub-repo.yml b/artifacthub-repo.yml new file mode 100644 index 0000000000..00691b697e --- /dev/null +++ b/artifacthub-repo.yml @@ -0,0 +1,19 @@ +# Artifact Hub repository metadata file +# +# Some settings like the verified publisher flag or the ignored packages won't +# be applied until the next time the repository is processed. Please keep in +# mind that the repository won't be processed if it has not changed since the +# last time it was processed. Depending on the repository kind, this is checked +# in a different way. For Helm http based repositories, we consider it has +# changed if the `index.yaml` file changes. For git based repositories, it does +# when the hash of the last commit in the branch you set up changes. This does +# NOT apply to ownership claim operations, which are processed immediately. +# +repositoryID: 1b4e2e51-4979-43c4-a164-c26426c196b2 +owners: # (optional, used to claim repository ownership) + - name: meshery + email: maintainers@meshery.io +#ignore: # (optional, packages that should not be indexed by Artifact Hub) +# - name: package1 +# - name: package2 # Exact match +# version: beta # Regular expression (when omitted, all versions are ignored) \ No newline at end of file diff --git a/build/component_info.json b/build/component_info.json new file mode 100644 index 0000000000..17b71b91fe --- /dev/null +++ b/build/component_info.json @@ -0,0 +1,5 @@ +{ + "name": "helm-kanvas-snapshot", + "type": "component", + "next_error_code": 1000 +} diff --git a/cmd/kanvas-snapshot/main.go b/cmd/kanvas-snapshot/cmd.go similarity index 99% rename from cmd/kanvas-snapshot/main.go rename to cmd/kanvas-snapshot/cmd.go index 5c72ec80ad..ddc2df000a 100644 --- a/cmd/kanvas-snapshot/main.go +++ b/cmd/kanvas-snapshot/cmd.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "bytes" @@ -38,7 +38,7 @@ var generateKanvasSnapshotCmd = &cobra.Command{ Use: "kanvas", Short: "Generate a Kanvas snapshot using a Helm chart", Long: `Generate a Kanvas snapshot by providing a Helm chart URI. - + This command allows you to generate a snapshot in Meshery using a Helm chart. Example usage: @@ -206,7 +206,6 @@ func CreateMesheryDesign(uri, name, email string) (string, error) { } func GenerateSnapshot(designID, chartURI, email, assetLocation string) error { - payload := map[string]interface{}{ "Payload": map[string]string{ "application_type": "Helm Chart", @@ -264,8 +263,7 @@ func isValidEmail(email string) bool { return emailRegex.MatchString(email) } -func main() { - +func Main() { generateKanvasSnapshotCmd.Flags().StringVarP(&chartURI, "file", "f", "", "URI to Helm chart (required)") generateKanvasSnapshotCmd.Flags().StringVarP(&designName, "design-name", "n", "", "Optional name for the Meshery design") generateKanvasSnapshotCmd.Flags().StringVarP(&email, "email", "e", "", "Optional email to associate with the Meshery design") @@ -277,7 +275,6 @@ func main() { Log.Error(err) os.Exit(1) } - } func init() { diff --git a/go.mod b/go.mod index a61a694817..3a7cc30d5e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.8 require ( github.com/layer5io/meshkit v0.7.78 github.com/sirupsen/logrus v1.9.3 + github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 ) @@ -21,7 +22,6 @@ require ( github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect - github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.6.0 // indirect go.uber.org/multierr v1.11.0 // indirect diff --git a/helm-kanvas-snapshot b/helm-kanvas-snapshot new file mode 100755 index 0000000000..b3c332405b Binary files /dev/null and b/helm-kanvas-snapshot differ diff --git a/install-binary.sh b/install-binary.sh new file mode 100644 index 0000000000..4fe7c5f0fc --- /dev/null +++ b/install-binary.sh @@ -0,0 +1,100 @@ +#! /bin/bash -e + +function handle_exit() { + result=$? + if [ "$result" != "0" ]; then + printf "Failed to install helm-kanvas-snapshot plugin\n" + fi + exit $result +} + +function normalize_architecture() { + arch=$1 + + case "$arch" in + "aarch64") + echo "arm64" + ;; + *) + echo $arch + ;; + esac +} + +function download_plugin() { + OUTPUT_BASENAME=helm-kanvas-snapshot + + if [[ -n "$LOCAL_FILE_PATH" ]]; then + OUTPUT_BASENAME_WITH_POSTFIX="$LOCAL_FILE_PATH" + echo -e "Using local archive at ${OUTPUT_BASENAME_WITH_POSTFIX}\n" + else + version=$(grep version "$HELM_PLUGIN_DIR/plugin.yaml" | cut -d'"' -f2) + DOWNLOAD_URL="https://github.com/meshery/helm-kanvas-snapshot/releases/download/v$version/helm-kanvas-snapshot_${version}_${os_name}_${os_arch}.tar.gz" + OUTPUT_BASENAME_WITH_POSTFIX="$HELM_PLUGIN_DIR/$OUTPUT_BASENAME.tar.gz" + + echo -e "Download URL set to ${DOWNLOAD_URL}\n" + echo -e "Artifact path: ${OUTPUT_BASENAME_WITH_POSTFIX}\n" + + if [[ -n $(command -v curl) ]]; then + if curl --fail -L "${DOWNLOAD_URL}" -o "${OUTPUT_BASENAME_WITH_POSTFIX}"; then + echo -e "Successfully downloaded the archive, proceeding to install\n" + else + echo -e "Failed while downloading helm-kanvas-snapshot archive\n" + exit 1 + fi + else + echo "curl is required to download the plugin" + exit -1 + fi + fi +} + + +function install_plugin() { + local HELM_PLUGIN_ARTIFACT_PATH=${OUTPUT_BASENAME_WITH_POSTFIX} + local PROJECT_NAME="helm-kanvas-snapshot" + local HELM_PLUGIN_TEMP_PATH="/tmp/$PROJECT_NAME" + + echo -n "HELM_PLUGIN_ARTIFACT_PATH: ${HELM_PLUGIN_ARTIFACT_PATH}" + rm -rf "${HELM_PLUGIN_TEMP_PATH}" + + echo -e "Preparing to install into ${HELM_PLUGIN_DIR}\n" + mkdir -p "${HELM_PLUGIN_TEMP_PATH}" + tar -xvf "${HELM_PLUGIN_ARTIFACT_PATH}" -C "${HELM_PLUGIN_TEMP_PATH}" + mkdir -p "$HELM_PLUGIN_DIR/bin" + mv "${HELM_PLUGIN_TEMP_PATH}/helm-kanvas-snapshot" "${HELM_PLUGIN_DIR}/bin/helm-kanvas-snapshot" + rm -rf "${HELM_PLUGIN_TEMP_PATH}" + rm -rf "${HELM_PLUGIN_ARTIFACT_PATH}" +} + +function install() { + echo "Installing helm-kanvas-snapshot..." + + download_plugin + status=$? + if [ $status -ne 0 ]; then + echo -e "Downloading plugin failed\n" + exit 1 + fi + + set +e + install_plugin + local install_status=$? + set -e + + if [ "$install_status" != "0" ]; then + echo "Installing helm-kanvas-snapshot plugin failed with error code: ${install_status}" + exit 1 + fi + + echo + echo "helm-kanvas-snapshot is installed." + echo + "${HELM_PLUGIN_DIR}/bin/helm-kanvas-snapshot" -h + echo + echo "See https://github.com/meshery/helm-kanvas-snapshot#readme for more information on getting started." +} + +trap "handle_exit" EXIT + +install "$@" diff --git a/internal/log/logger.go b/internal/log/logger.go index 42a96f0d61..129904dabe 100644 --- a/internal/log/logger.go +++ b/internal/log/logger.go @@ -20,7 +20,7 @@ func (f *TerminalFormatter) Format(entry *log.Entry) ([]byte, error) { // Call this function to setup logrus func SetupLogrusFormatter() { - //log formatter for improved UX + // log formatter for improved UX log.SetFormatter(new(TerminalFormatter)) } diff --git a/main.go b/main.go new file mode 100644 index 0000000000..ea24cb7621 --- /dev/null +++ b/main.go @@ -0,0 +1,9 @@ +package main + +import ( + cmd "github.com/meshery/helm-kanvas-snapshot/cmd/kanvas-snapshot" +) + +func main() { + cmd.Main() +} diff --git a/plugin.yaml b/plugin.yaml index 71bbf4fde2..e3fb36cb12 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -1,20 +1,15 @@ -name: "kanvas-snapshot" +name: "helm-kanvas-snapshot" version: "0.1.0" usage: "Generate a visual snapshot of your Helm chart as a Meshery Snapshot" description: "A Helm plugin to generate Meshery Snapshots from Helm charts using a custom Go binary" -command: "$HELM_PLUGIN_DIR/bin/kanvas-snapshot-linux-amd64" -platformCommand: - - os: linux - arch: amd64 - command: "$HELM_PLUGIN_DIR/bin/kanvas-snapshot-linux-amd64" - - os: darwin - arch: amd64 - command: "$HELM_PLUGIN_DIR/bin/kanvas-snapshot-darwin-amd64" - - os: windows - arch: amd64 - command: "$HELM_PLUGIN_DIR/bin/kanvas-snapshot-windows-amd64.exe" +useTunnel: true +command: "$HELM_PLUGIN_DIR/bin/helm-kanvas-snapshot" hooks: install: | echo "Snapshot plugin installed." + + update: | + echo "Snapshot plugin updated." + $HELM_PLUGIN_DIR/install-binary.sh uninstall: | echo "Snapshot plugin uninstalled."