ichigeki

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: MIT Imports: 14 Imported by: 0

README

ichigeki

Documentation Latest GitHub release Github Actions test Go Report Card License

ichigeki is the util tool for one time script for mission critical (especially for preventing rerunning it). this tool inspired by perl's Script::Ichigeki

Usage as a CLI

for ECS Task. log output to S3 Bucket. For example, for a one time script that needs to be executed on 2022-06-01

$ ichigeki --s3-url-prefix s3://ichigeki-example-com/logs/ --exec-date 2022-06-01 --no-confirm-dialog -- your_command

or with default config ~/.config/ichigeki/default.toml.

confirm_dialog = false
default_name_template = "{{ .Name }}{{ if gt (len .Args) 1}}-{{ .Args | hash }}{{ end }}"

[s3]
bucket = "ichigeki-example-com"
object_prefix = "logs/"
$ ichigeki --exec-date 2022-06-01 -- your_command
ICHIGEKI_EXECUTION_ENVs

If you want to check whether the command is started using ichigeki on the side of the command to be started, you can check the environment variable named ICHIGEKI_EXECUTION_ENV. If version information is stored, it is invoked via ichigeki command.

sample.sh

#!/bin/bash

echo $ICHIGEKI_EXECUTION_ENV
echo $ICHIGEKI_EXECUTION_NAME
echo $ICHIGEKI_EXECUTION_DATE
$ ichigeki --s3-url-prefix s3://ichigeki-example-com/logs/  -- ./sample.sh           
[info] log output to `s3://ichigeki-example-com/logs/sample.sh.log`
ichigeki v0.3.0 
default_name_template in ~/.config/ichigeki/default.toml

The default configuration file provides a template for dynamically determining the ichigeki name. This dynamic ichigeki name mechanism works only if you do not explicitly specify -name in the options This template follows the Go template notation text/template

The following data is passed to the template:

  • .Name : Default name if template is not specified
  • .ExecDate: -exec-date or the value of the execution date. format(2016-01-02)
  • .Today: the value of the execution date. format(2016-01-02)
  • .Args: A space-separated array of the commands passed. (type []string)

The following custom functions are passed:

  • sha256 : Given a string or []string, compute the hexadecimal notation of sha256 hash
  • hash : First 7 characters of the hexadecimal notation of the sha256 hash
  • arg : Value of the specified index of .Args. If not present, it will be an empty string. (sample {{ arg 1 }})
  • last_arg : Last element of .Args
  • env : Refers to the environment variable at the start of execution. If not set, an empty character will be returned.
  • must_env : Refers to the environment variable at the start of execution. If it is not set, it will panic.

For example: default_name_template = "{{ .Name }}{{ if gt (len .Args) 1}}-{{ .Args | hash }}{{ end }}"

$ ichigeki -- ./sample.sh => sample.sh $ ichigeki -- go run cmd/migration/. --debug => go-4575533

Install
Homebrew (macOS and Linux)
$ brew install mashiike/tap/ichigeki
Binary packages

Releases

Options
ichigeki [options] -- (commands)
  -dir string
        log destination for s3
  -exec-date string
        scheduled execution date
  -name string
        ichigeki name
  -no-confirm-dialog
        do confirm
  -s3-url-prefix string
        log destination for s3

Usage as a library

for example:

package main

import (
    "fmt"
    "log"

    "github.com/mashiike/ichigeki"
    "github.com/mashiike/ichigeki/s3log"
)


func main() {
    ld, err := s3log.New(context.Background(), &s3log.Config{
        Bucket:       "ichigeki-example-come",
        ObjectPrefix: "logs/",
    })      
    if err != nil {
        log.Fatal("s3 log destination:", err)
    }
    h := &ichigeki.Hissatsu{
        Name:           "hogehoge",
        LogDestination: ld,
        ConfirmDialog:  ichigeki.Bool(true),
        Script: func(_ context.Context, stdout io.Writer, stderr io.Writer) error {
            fmt.Fprintln(stdout, "this message out to stdout") 
            fmt.Fprintln(stderr, "this message out to stderr") 
            return nil 
        }, 
    }

    if err := h.Execute(); err != nil {
        log.Fatal(err)
    }
}

LICENSE

MIT License

Copyright (c) 2022 IKEDA Masashi

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(b bool) *bool

Types

type Context added in v0.3.0

type Context struct {
	context.Context
	Name     string
	ExecDate string
}

type Hissatsu

type Hissatsu struct {
	Name                string
	DefaultNameTemplate string
	Logger              *log.Logger
	Args                []string
	Description         string
	ExecDate            time.Time
	ConfirmDialog       *bool
	LogDestination      LogDestination
	Script              ScriptFunc
	DialogMessage       string
	PromptInput         io.Reader
	// contains filtered or unexported fields
}

func (*Hissatsu) Execute

func (h *Hissatsu) Execute() error

func (*Hissatsu) ExecuteWithContext

func (h *Hissatsu) ExecuteWithContext(ctx context.Context) (err error)

func (*Hissatsu) GenerateName added in v0.3.0

func (h *Hissatsu) GenerateName() error

func (*Hissatsu) Validate

func (h *Hissatsu) Validate() error

type LocalFile

type LocalFile struct {
	Path           string
	LogFilePostfix string
	// contains filtered or unexported fields
}

func (*LocalFile) AlreadyExists

func (f *LocalFile) AlreadyExists(_ context.Context) (bool, error)

func (*LocalFile) Cleanup

func (f *LocalFile) Cleanup(ctx context.Context)

func (*LocalFile) NewWriter

func (f *LocalFile) NewWriter(ctx context.Context) (io.Writer, io.Writer, error)

func (*LocalFile) SetName

func (f *LocalFile) SetName(name string)

func (*LocalFile) String

func (f *LocalFile) String() string

type LogDestination

type LogDestination interface {
	fmt.Stringer
	SetName(name string)
	AlreadyExists(ctx context.Context) (exists bool, err error)
	NewWriter(ctx context.Context) (stdout io.Writer, stderr io.Writer, err error)
	Cleanup(ctx context.Context)
}

type MultipleLogDestination

type MultipleLogDestination []LogDestination

func (MultipleLogDestination) AlreadyExists

func (mld MultipleLogDestination) AlreadyExists(ctx context.Context) (bool, error)

func (MultipleLogDestination) Cleanup

func (mld MultipleLogDestination) Cleanup(ctx context.Context)

func (MultipleLogDestination) NewWriter

func (mld MultipleLogDestination) NewWriter(ctx context.Context) (io.Writer, io.Writer, error)

func (MultipleLogDestination) SetName

func (mld MultipleLogDestination) SetName(name string)

func (MultipleLogDestination) String

func (mld MultipleLogDestination) String() string

type ScriptFunc added in v0.3.0

type ScriptFunc func(ctx Context, stdout io.Writer, stderr io.Writer) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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