Golang SDK

The Golang version of The Ops Platform SDK works in conjunction with our Docker base images. The SDK allows the development of automations with rich user interfaces, that run in both the local terminal and in our Slack App as Slack Ops.

Supported methods

The following SDK methods are supported in Golang:

  • GetHostOS
  • GetInterfaceType
  • GetSecret
  • GetStatePath
  • HomeDir
  • SetSecret
  • Track
  • GetState
  • SetState
  • GetConfig
  • GetAllConfig
  • SetConfig

To use the SDK, you need to create a new SDK object:

func NewSdk() *Sdk

Alternatively, the SDK object is included in the Client object:

client := ctoai.NewClient()

func (*Sdk) GetHostOS

func (*Sdk) GetHostOS() string

Returns the current host OS.

Example

package main

import (
	ctoai "github.com/cto-ai/sdk-go"
)

func main() {
	client := ctoai.NewClient()
	o := client.Sdk.GetHostOS()
	client.Ux.Print(o)
}

Output

darwin

func (*Sdk) GetInterfaceType

func (*Sdk) GetInterfaceType() string

Returns the interface type that the Op is attached to (e.g. terminal or Slack).

Example

package main

import (
	ctoai "github.com/cto-ai/sdk-go"
)

func main() {
	client := ctoai.NewClient()
	o := client.Sdk.GetInterfaceType()
	client.Ux.Print(o)
}

Output

terminal

func (*Sdk) GetSecret

func (*Sdk) GetSecret(key string) (string, error)

Requests a secret from the secret store by key.

If the secret exists, it is returned, with the daemon notifying the user that it is in use.

Otherwise, the user is prompted to provide a replacement.

Example

package main

import (
	ctoai "github.com/cto-ai/sdk-go"
)

func main() {
	client := ctoai.NewClient()
	o, err := client.Sdk.GetSecret("sec1")
	client.Ux.Print(o)
	if err != nil {
		panic(err)
	}
}

Output

Failed to retrieve secret sec1!
We were unable to find a secret with key sec1. Please select an alternate key or enter a value.
Enter a value:

func (*Sdk) HomeDir

func (*Sdk) HomeDir() string

Returns the location of the user home directory.

Example

package main

import (
	ctoai "github.com/cto-ai/sdk-go"
)

func main() {
	client := ctoai.NewClient()
	o := client.Sdk.HomeDir()
	client.Ux.Print(o)
}

Output

/root

func (*Sdk) SetSecret

func (*Sdk) SetSecret(key string, value string) (string, error)

Sets a particular value into the secret store.

If the secret already exists, the user is prompted on whether to overwrite it.

Example

package main

import (
	ctoai "github.com/cto-ai/sdk-go"
)

func main() {
	client := ctoai.NewClient()
	s,err := client.Sdk.SetSecret("sec1", "password1")
	client.Ux.Print(s)
	if err != nil {
		panic(err)
	}
}

Output

sec1

func (*Sdk) Track

func (*Sdk) Track(tags []string, event string, metadata map[string]interface{}) error

Sends an event to the CTO.ai workflow event system.

This is the public facing API to enable developers to send workflow events.

Example

s := ctoai.NewSdk()
err := s.Track([]string{"sdk", "go", "tracked"}, "testing", map[string]interface{}{
    "user": "name",
})

The event, tags, and payload will be sent to the events system.

func (*Sdk) Events

Note: This feature is coming soon

func (*Sdk) Events(tags []string, team string, metadata map[string]interface{}) error

Retrieves events to the CTO.ai analytics system.

This is the public facing API to enable developers to retrieve workflow events.

Example

s := ctoai.NewSdk()
err := s.Events([]string{"sdk", "backend", "deploy"}, "test", map[string]interface{}{
    "image": "jifejfdkdkdi3930",
})

The event, tags, and payload will be retrieved from the events system.

func (s *Sdk) GetConfig

func (s *Sdk) GetConfig(key string) (string, error) 

Returns a value from the team configuration store. If the value does not exist, returns an empty string.

Example

s := ctoai.NewSdk()
configValue, err := s.GetConfig("relevant_configuration")

func (s *Sdk) GetAllConfig

func (s *Sdk) GetAllConfig() (map[string]string, error) 

Returns the contents of the team configuration store, as a map of keys to values.

Example

s := ctoai.NewSdk()
fullConfig, err := s.GetAllConfig()

func (s *Sdk) SetConfig

func (s *Sdk) SetConfig(key string, value string) error 

Sets a value in the team configuration store.

Example

s := ctoai.NewSdk()
err := s.SetConfig("AWS_PROFILE", "default")