cmd

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 10 Imported by: 6

Documentation

Overview

Package cmd provides an opiniated interface for implementing Axiom based tools and utilities. Those tools share the same configuration, logging and application lifecycle behaviour. This results in an easy to use and understandable set of application.

In the most basic case, applications should pass their name and an implementation of `cmd.RunFunc` to `cmd.Run()`:

package main

import (
    "context"

    "github.com/axiomhq/pkg/cmd"
)

func main() {
    cmd.Run("my-app", Run)
}

func Run(_ context.Context, log *zap.Logger, _ *axiom.Client) error {
    log.Info("hello, world!")

    return nil
}
Example
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/axiomhq/axiom-go/axiom"
	"go.uber.org/zap"

	"github.com/axiomhq/pkg/cmd"
)

func main() {
	os.Clearenv()
	os.Setenv("DEBUG", "1")

	mainFunc := func(_ context.Context, _ *zap.Logger, _ *axiom.Client) error {
		// All your actual application code goes here! See doc.go for more info.

		fmt.Print("Hello World!")

		return nil
	}

	cmd.Run("example", mainFunc,
		cmd.WithAxiomOptions(
			axiom.SetNoEnv(),
			axiom.SetURL("http://axiom.local"),
			axiom.SetAccessToken("xapt-1234"),
		),
	)

}
Output:

Hello World!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultExitSignals

func DefaultExitSignals() []os.Signal

DefaultExitSignals are the default signals to catch and exit upon.

func DefaultLoggerOptions

func DefaultLoggerOptions() []zap.Option

DefaultLoggerOptions are the default logger options to use

func Error

func Error(msg string, err error, fields ...zap.Field) error

Error is a convenience function that improves error log output when returning from the `MainFunc`.

func Run

func Run(appName string, fn RunFunc, options ...Option)

Run the named app with the given `RunFunc`. Additionally, options can be passed to configure the behaviour of the bootstrapping process.

Types

type Option

type Option func(c *config) error

An Option modifies the behaviour of the `Run()` function.

func WithAxiomOptions

func WithAxiomOptions(options ...axiom.Option) Option

WithAxiomOptions sets the options used for creating the Axiom client.

func WithExitSignals

func WithExitSignals(signals ...os.Signal) Option

WithExitSignals sets the signals that will cause the program to exit gracefully. If this option is not specified, the default signals are specified by the `DefaultExitSignals()` function.

func WithLoggerOptions

func WithLoggerOptions(options ...zap.Option) Option

WithLoggerOptions sets the options used for creating the logger. If this option is not specified, the default logger options are specified by the `DefaultLoggerOptions()` function.

func WithRequiredEnvVars

func WithRequiredEnvVars(envVars ...string) Option

WithRequiredEnvVars sets the environment variables that are required to be set at application startup. Required environment variables must be set and not be empty. "AXIOM_URL", "AXIOM_TOKEN", "AXIOM_ORG_ID" and "DEBUG" are reserved.

func WithValidateAxiomCredentials

func WithValidateAxiomCredentials() Option

WithValidateAxiomCredentials will validate the Axiom credentials at startup and fail the execution gracefully, if they are invalid.

type RunFunc

type RunFunc func(context.Context, *zap.Logger, *axiom.Client) error

RunFunc is implemented by the main packages and passed to the `Run` function which takes care of signal handling, loading the runtime configuration and setting up logging, the Axiom client, etc. It must block until the context is marked done. Errors returned from the `RunFunc` should be created using the `Error()` function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL