github.com/informationsea/shellflow@v0.1.3/configuration.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/BurntSushi/toml"
    12  )
    13  
    14  type Memory struct {
    15  	memoryByte int64
    16  }
    17  
    18  func (m Memory) MarshalJSON() ([]byte, error) {
    19  	return json.Marshal(m.memoryByte)
    20  }
    21  
    22  func (m *Memory) UnmarshalJSON(data []byte) error {
    23  	return json.Unmarshal(data, &m.memoryByte)
    24  }
    25  
    26  func mustParseNewMemory(s string) Memory {
    27  	m, e := NewMemory(s)
    28  	if e != nil {
    29  		panic(e)
    30  	}
    31  	return m
    32  }
    33  
    34  func NewMemory(s string) (Memory, error) {
    35  	var m float64
    36  	var multiply float64 = 1
    37  	var e error
    38  	if strings.HasSuffix(s, "G") {
    39  		m, e = strconv.ParseFloat(s[:len(s)-1], 64)
    40  		multiply = 1024 * 1024 * 1024
    41  	} else if strings.HasSuffix(s, "M") {
    42  		m, e = strconv.ParseFloat(s[:len(s)-1], 64)
    43  		multiply = 1024 * 1024
    44  	} else if strings.HasSuffix(s, "K") || strings.HasSuffix(s, "k") {
    45  		m, e = strconv.ParseFloat(s[:len(s)-1], 64)
    46  		multiply = 1024
    47  	} else {
    48  		m, e = strconv.ParseFloat(s[:len(s)], 64)
    49  	}
    50  
    51  	if e != nil {
    52  		return Memory{}, e
    53  	}
    54  	return Memory{int64(m * multiply)}, nil
    55  }
    56  
    57  func (m Memory) Byte() int64 {
    58  	return m.memoryByte
    59  }
    60  
    61  func (m Memory) KiloByte() float32 {
    62  	return float32(m.memoryByte) / 1024
    63  }
    64  
    65  func (m Memory) MegaByte() float32 {
    66  	return m.KiloByte() / 1024
    67  }
    68  
    69  func (m Memory) GigaByte() float32 {
    70  	return m.MegaByte() / 1024
    71  }
    72  
    73  func (m Memory) String() string {
    74  	if m.memoryByte > 1024*1024*1024 {
    75  		return fmt.Sprintf("%.2fG", m.GigaByte())
    76  	}
    77  	if m.memoryByte > 1024*1024 {
    78  		return fmt.Sprintf("%.2fM", m.MegaByte())
    79  	}
    80  	if m.memoryByte > 1024 {
    81  		return fmt.Sprintf("%.2fk", m.KiloByte())
    82  	}
    83  	return fmt.Sprintf("%d", m.Byte())
    84  }
    85  
    86  type CommandConfiguration struct {
    87  	RegExp          string
    88  	SGEOption       []string
    89  	DontInheirtPath bool
    90  	RunImmediate    bool
    91  }
    92  
    93  func (v *CommandConfiguration) String() string {
    94  	return fmt.Sprintf("SGEOption: %s / DontInheirtPath: %t / RunImmediate: %t", v.SGEOption, v.DontInheirtPath, v.RunImmediate)
    95  }
    96  
    97  type Backend struct {
    98  	Type string
    99  }
   100  
   101  type Configuration struct {
   102  	Environment map[string]string
   103  	Backend     Backend
   104  	Command     []CommandConfiguration
   105  }
   106  
   107  //go:generate go-assets-builder --package=main --output=assets.go default_config.toml
   108  
   109  var ShellflowConfig = os.ExpandEnv("${HOME}/.shellflow.toml")
   110  
   111  func LoadConfiguration() (*Configuration, error) {
   112  	// prefer local conf file
   113  	localConfFile, err := os.Open("shellflow.toml")
   114  	if err == nil {
   115  		var conf Configuration
   116  		_, err = toml.DecodeReader(localConfFile, &conf)
   117  		if err != nil {
   118  			return nil, fmt.Errorf("cannot read local TOML. %s", err.Error())
   119  		}
   120  		return &conf, nil
   121  	}
   122  
   123  	confFile, err := os.Open(ShellflowConfig)
   124  	if err == nil {
   125  		defer confFile.Close()
   126  	} else if os.IsNotExist(err) {
   127  		// copy default config file from asset
   128  		defaultConf, err := Assets.Open("/default_config.toml")
   129  		if err != nil {
   130  			return nil, fmt.Errorf("Cannot open default config.: %s", err.Error())
   131  		}
   132  		defer defaultConf.Close()
   133  		confFileToWrite, err := os.OpenFile(ShellflowConfig, os.O_CREATE|os.O_WRONLY, 0640)
   134  		if err != nil {
   135  			return nil, err
   136  		}
   137  		defer confFileToWrite.Close()
   138  		_, err = io.Copy(confFileToWrite, defaultConf)
   139  		if err != nil {
   140  			return nil, fmt.Errorf("Cannot write default config data. %s", err.Error())
   141  		}
   142  		err = confFileToWrite.Sync()
   143  		if err != nil {
   144  			return nil, fmt.Errorf("Cannot sync config data. %s", err.Error())
   145  		}
   146  
   147  		confFile, err = os.Open(ShellflowConfig)
   148  		if err != nil {
   149  			return nil, fmt.Errorf("Cannot open config file for read. %s", err.Error())
   150  		}
   151  		defer confFile.Close()
   152  	} else if err != nil {
   153  		return nil, err
   154  	}
   155  
   156  	var conf Configuration
   157  	_, err = toml.DecodeReader(confFile, &conf)
   158  	if err != nil {
   159  		return nil, fmt.Errorf("cannot read TOML. %s", err.Error())
   160  	}
   161  	return &conf, nil
   162  }