config

package
v0.0.0-...-1b113c7 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package config consists of main functionality for working with all configuration constants and initialisation struct.

Priority:

  • OS ENV

  • Flags

  • default values

Example (ConfigOrder)
package main

import (
	"fmt"
	"os"

	"github.com/KusoKaihatsuSha/tray_helper/internal/config"
	"github.com/KusoKaihatsuSha/tray_helper/internal/helpers"
)

func main() {
	testDataFlag := `{
                 "address": "http://file-flag"
         }`
	testDataEnv := `{
                 "address": "http://file-env"
         }`
	fileFlag := helpers.CreateTmp()
	err := os.WriteFile(fileFlag, []byte(testDataFlag), 0755)
	if err != nil {
		fmt.Println(err)
	}
	fileEnv := helpers.CreateTmp()
	err = os.WriteFile(fileEnv, []byte(testDataEnv), 0755)

	if err != nil {
		fmt.Println(err)
	}

	err = os.Setenv("ADDRESS", "http://env")
	if err != nil {
		fmt.Println(err)
	}
	err = os.Setenv("CONFIG", fileEnv)
	if err != nil {
		fmt.Println(err)
	}

	os.Args = append(
		os.Args,
		"-config="+fileFlag,
		"-a=http://flag",
	)
	testConfiguration := config.Init()
	testConfiguration.Init()

	testConfiguration.Flag() // preload
	testConfiguration.Env()  // preload

	// test section
	testConfiguration.Conf() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Flag() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Env() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Conf()
	testConfiguration.Flag() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Conf()
	testConfiguration.Env() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Flag()
	testConfiguration.Env() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Conf()
	testConfiguration.Flag()
	testConfiguration.Env() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Flag()
	testConfiguration.Conf()
	testConfiguration.Env() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Conf()
	testConfiguration.Env()
	testConfiguration.Flag() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Env()
	testConfiguration.Conf()
	testConfiguration.Flag() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Flag()
	testConfiguration.Env()  // last filepath config here
	testConfiguration.Conf() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	testConfiguration.Env()
	testConfiguration.Flag() // last filepath config here
	testConfiguration.Conf() // <--
	testConfiguration.Valid()
	fmt.Println(testConfiguration.Address)

	helpers.DeleteTmp(fileEnv)
	helpers.DeleteTmp(fileFlag)

}
Output:

file-env:80
flag:80
env:80
flag:80
env:80
env:80
env:80
env:80
flag:80
flag:80
file-env:80
file-flag:80

Index

Examples

Constants

View Source
const (
	DefTimePostfix = "s"
	HTTPString     = `http://`
	HTTPSString    = `https://`

	RealIPName  = "X-Real-IP"
	IcoNotif    = `icons/notif.ico`
	IcoApp      = `icons/icon.ico`
	IcoSettings = `icons/settings.ico`
	IcoClose    = `icons/close.ico`
	Title       = "Easy tray helper"
	ToolTip     = "<" + Title + "> make you the daily work some easy"

	Repeat         = "repeat"
	Silent         = "silent"
	WaitUntilClose = "timer"
	EmulateAction  = "actions"
)

General annotation.

Variables

View Source
var EmbedFiles embed.FS

Functions

func Set

func Set(c Configuration)

Set change conf.

func SettingsFile

func SettingsFile(filename string) (compare map[string]any)

SettingsFile return map[string]any from the setting file

Types

type Configuration

type Configuration struct {
	PrefixURL string
	Tmp
	ConfigurationMain
}

Configuration consist settings, which filling on the start. valid list:

       "tmp_file"  - Crop prefix path and add tmp path for windows. For linux same string
		  "file"      - Check exist and return correct /\
		  "url"       - Check correct URL
		  "bool"      - Check correct bool
		  "timer"     - Check correct timer. If not included "s"... suffix
       "-", "none" - Nothing to do. Same string

func Get

func Get() Configuration

Get create new conf.

func Init

func Init() Configuration

Init if not need use init()

func (*Configuration) Conf

func (c *Configuration) Conf()

Conf get info from the configuration file

func (*Configuration) Dummy

func (c *Configuration) Dummy()

Dummy needed for the default printing of Flags info

func (*Configuration) Env

func (c *Configuration) Env()

Env get info from the Environment

func (*Configuration) Flag

func (c *Configuration) Flag()

Flag get info from the Flags

func (*Configuration) Init

func (c *Configuration) Init()

Init find the info inside the tags

func (*Configuration) Valid

func (c *Configuration) Valid()

Valid check info and make some correcting

type ConfigurationMain

type ConfigurationMain struct {
	Config  string `json:"config" default:"settings.data" flag:"f,c,config" env:"CONFIG" text:"Config file" valid:"file"`
	Ico     string `json:"ico_file" default:"app.ico" flag:"ico" env:"ICO_FILE" text:"Filepath for ico notif" valid:"tmp_file"`
	Address string `json:"address" default:"ip:0" flag:"a" env:"ADDRESS" text:"Address and port" valid:"url"`
}

ConfigurationMain - block consist the main settings

type CtxKey

type CtxKey string

type Tag

type Tag struct {
	// contains filtered or unexported fields
}

Tag store general values

type TagInfo

type TagInfo struct {
	ConfigFile func(map[string]any) TagInfo
	Valid      func() any
	DummyFlags func() TagInfo
	Env        func() TagInfo
	Flag       func() TagInfo

	Tag
	// contains filtered or unexported fields
}

TagInfo store tags Config struct

type Tmp

type Tmp struct {
	// contains filtered or unexported fields
}

Tmp struct consist tmp information when reading/valid configs

Jump to

Keyboard shortcuts

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