Skip to content

Commit

Permalink
Merge pull request #1 from coherenceplatform/aa/initial-setup
Browse files Browse the repository at this point in the history
[WIT-1159] cocli v1
  • Loading branch information
zach-withcoherence authored Nov 10, 2023
2 parents 0c5c787 + 1e09c4c commit aecf6d2
Show file tree
Hide file tree
Showing 24 changed files with 1,007 additions and 0 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/build-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Build and Release

on:
push:
branches:
- main

jobs:
build-and-release:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21

- name: Build CLI
run: |
./cocli_build.sh cocli
- name: Set Tag Name
id: set_tag_name
run: echo "TAG_NAME=$(cat cocli_version.txt)" >> $GITHUB_ENV

- name: Create Release
id: create_release
uses: actions/create-release@v1
with:
tag_name: v${{ env.TAG_NAME }}
release_name: Release v${{ env.TAG_NAME }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload Release Asset (darwin/arm64)
id: upload-release-asset-darwin-arm64
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./cocli-darwin-arm64
asset_name: cocli-darwin-arm64
asset_content_type: application/octet-stream
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload Release Asset (linux/arm64)
id: upload-release-asset-linux-arm64
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./cocli-linux-arm64
asset_name: cocli-linux-arm64
asset_content_type: application/octet-stream
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload Release Asset (windows/arm64)
id: upload-release-asset-windows-arm64
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./cocli-windows-amd64.exe
asset_name: cocli-windows-amd64.exe
asset_content_type: application/octet-stream
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
notes.txt
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
# cocli

## Installation

1. Go to [releases](https://github.com/coherenceplatform/cocli/releases) and download the binary for your os/architecture (e.g. linux/arm64)

2. Move the file into a location that is in your `$PATH` (e.g. /usr/local/bin). We recommend renaming the file `cocli`. e.g.
```bash
mv ~/Downloads/cocli-linux-arm64 /usr/local/bin/cocli
```

3. You'll likely need to update permissions to make the file executable:
```bash
chmod +x /usr/local/bin/cocli
```

4. Test that cocli works:
```bash
cocli -h
```

N.B. If you get an error trying to use the cli on a mac (along the lines of e.g. "...can’t be opened because apple cannot check it for malicious software.") then [follow these steps](https://support.apple.com/guide/mac-help/apple-cant-check-app-for-malicious-software-mchleab3a043/mac) to grant an exception for cocli.

## Development

By default, cocli is configured for production use. During development you should `export COHERENCE_ENVIRONMENT=review`.
To change your target environment update the `CoherenceDomain` in the `devConfig`. It can be found in `pkg/cocli/cocli.go`.

If you are targeting a workspace, the cli will need to use a slightly different api url path. This will be handled automatically by cocli.

### Versioning

To update the cocli version it needs to be set in 2 places:
- update the `cliVersion` constant in `pkg/cocli/cocli.go`
- update the version in `cli_version.txt`

In addition to the above, the cli api version should be updated in the control-plane metadata response.
14 changes: 14 additions & 0 deletions cmd/cocli/apps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cocli

import (
"github.com/spf13/cobra"
)

var applicationCmd = &cobra.Command{
Use: "apps",
Short: "Coherence application management commands",
}

func init() {
applicationCmd.AddCommand(listAppsCmd)
}
55 changes: 55 additions & 0 deletions cmd/cocli/archiveFeature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cocli

import (
"bytes"
"encoding/json"
"fmt"
"io"

"github.com/coherenceplatform/cocli/pkg/cocli"
"github.com/spf13/cobra"
)

var archiveFeatureId int

var archiveFeatureCmd = &cobra.Command{
Use: "archive",
Short: "Archive a coherence feature",
Run: func(cmd *cobra.Command, args []string) {
archiveFeaturePayload := map[string]string{
"status": "archived",
}
payloadBytes, err := json.Marshal(archiveFeaturePayload)
if err != nil {
panic(err)
}
payload := bytes.NewBuffer(payloadBytes)

featuresUpdateUrl := fmt.Sprintf(
"https://%s%s/features/%s",
cocli.GetCoherenceDomain(),
cocli.GetCoherenceApiPrefix(),
fmt.Sprint(archiveFeatureId),
)
res, err := cocli.CoherenceApiRequest(
"PUT",
featuresUpdateUrl,
payload,
)
if err != nil {
panic(err)
}
defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}

fmt.Println(cocli.FormatJSONOutput(bodyBytes))
},
}

func init() {
archiveFeatureCmd.Flags().IntVarP(&archiveFeatureId, "feature_id", "f", 0, "Feature ID (required)")
archiveFeatureCmd.MarkFlagRequired("feature_id")
}
17 changes: 17 additions & 0 deletions cmd/cocli/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cocli

import (
"github.com/spf13/cobra"
)

var authCmd = &cobra.Command{
Use: "auth",
Short: "Coherence authentication commands",
}

func init() {
authCmd.AddCommand(loginCmd)
authCmd.AddCommand(logoutCmd)
authCmd.AddCommand(printRefreshTokenCmd)
authCmd.AddCommand(getUserInfoCmd)
}
67 changes: 67 additions & 0 deletions cmd/cocli/createFeature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cocli

import (
"bytes"
"encoding/json"
"fmt"
"io"

"github.com/coherenceplatform/cocli/pkg/cocli"
"github.com/spf13/cobra"
)

var createFeatureAppId int
var createFeatureCommitSha string
var createFeatureName string

var createFeatureCmd = &cobra.Command{
Use: "create <branch_name>",
Short: "Create a new coherence feature",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
branchName := args[0]
if createFeatureName == "" {
createFeatureName = branchName
}

createFeaturePayloadData := map[string]string{
"application_id": fmt.Sprint(createFeatureAppId),
"branch_name": branchName,
"commit_sha": createFeatureCommitSha,
"name": createFeatureName,
}
payloadBytes, err := json.Marshal(createFeaturePayloadData)
if err != nil {
panic(err)
}
payload := bytes.NewBuffer(payloadBytes)

featuresCreateUrl := fmt.Sprintf(
"https://%s%s/features",
cocli.GetCoherenceDomain(),
cocli.GetCoherenceApiPrefix(),
)
res, err := cocli.CoherenceApiRequest(
"POST",
featuresCreateUrl,
payload,
)
if err != nil {
panic(err)
}
defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}

fmt.Println(cocli.FormatJSONOutput(bodyBytes))
},
}

func init() {
createFeatureCmd.Flags().IntVarP(&createFeatureAppId, "app_id", "a", 0, "App ID (required)")
createFeatureCmd.MarkFlagRequired("app_id")
createFeatureCmd.Flags().StringVarP(&createFeatureCommitSha, "commit_sha", "c", "", "Commit SHA (optional)")
createFeatureCmd.Flags().StringVarP(&createFeatureName, "name", "n", "", "Feature name (optional - defaults to branch_name)")
}
36 changes: 36 additions & 0 deletions cmd/cocli/currentUser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cocli

import (
"fmt"
"io"

"github.com/coherenceplatform/cocli/pkg/cocli"
"github.com/spf13/cobra"
)

var currentUserCmd = &cobra.Command{
Use: "current_user",
Short: "List authenticated coherence user information",
Run: func(cmd *cobra.Command, args []string) {
currentUserUrl := fmt.Sprintf(
"https://%s%s/current_user",
cocli.GetCoherenceDomain(),
cocli.GetCoherenceApiPrefix(),
)
res, err := cocli.CoherenceApiRequest(
"GET",
currentUserUrl,
nil,
)
if err != nil {
panic(err)
}
defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}

fmt.Println(cocli.FormatJSONOutput(bodyBytes))
},
}
16 changes: 16 additions & 0 deletions cmd/cocli/features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cocli

import (
"github.com/spf13/cobra"
)

var featureCmd = &cobra.Command{
Use: "features",
Short: "Coherence feature management commands",
}

func init() {
featureCmd.AddCommand(listFeaturesCmd)
featureCmd.AddCommand(createFeatureCmd)
featureCmd.AddCommand(archiveFeatureCmd)
}
37 changes: 37 additions & 0 deletions cmd/cocli/listApps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cocli

import (
"fmt"
"io"

"github.com/coherenceplatform/cocli/pkg/cocli"
"github.com/spf13/cobra"
)

var listAppsCmd = &cobra.Command{
Use: "list",
Short: "List all coherence applications",
Long: "List all coherence applications that are accessible by the currently authenticated user.",
Run: func(cmd *cobra.Command, args []string) {
appsListUrl := fmt.Sprintf(
"https://%s%s/applications",
cocli.GetCoherenceDomain(),
cocli.GetCoherenceApiPrefix(),
)
res, err := cocli.CoherenceApiRequest(
"GET",
appsListUrl,
nil,
)
if err != nil {
panic(err)
}
defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}

fmt.Println(cocli.FormatJSONOutput(bodyBytes))
},
}
Loading

0 comments on commit aecf6d2

Please sign in to comment.