main.go raw

   1  // This generator reads a podcfg.Configs map and spits out a podcfg.Config struct
   2  package main
   3  
   4  import (
   5  	"fmt"
   6  	"go/format"
   7  	"io/ioutil"
   8  	"os"
   9  	"path/filepath"
  10  	"reflect"
  11  	"sort"
  12  
  13  	"github.com/p9c/p9/pod/config"
  14  	"github.com/p9c/p9/pod/podcfgs"
  15  )
  16  
  17  func main() {
  18  	c := podcfgs.GetConfigs()
  19  	var o string
  20  	var cc config.ConfigSlice
  21  	for i := range c {
  22  		cc = append(cc, config.ConfigSliceElement{Opt: c[i], Name: i})
  23  	}
  24  	sort.Sort(cc)
  25  	for i := range cc {
  26  		t := reflect.TypeOf(cc[i].Opt).String()
  27  		// W.Ln(t)
  28  		// split := strings.Split(t, "podcfg.")[1]
  29  		o += fmt.Sprintf("\t%s\t%s\n", cc[i].Name, t)
  30  	}
  31  	var e error
  32  	var out []byte
  33  	var wd string
  34  	generated := fmt.Sprintf(configBase, o)
  35  	if out, e = format.Source([]byte(generated)); E.Chk(e) {
  36  		// panic(e)
  37  		// fmt.Println(e)
  38  	}
  39  	if wd, e = os.Getwd(); E.Chk(e) {
  40  		// panic(e)
  41  	}
  42  	T.Ln("cwd",wd)
  43  	if e = ioutil.WriteFile(filepath.Join(wd, "struct.go"), out, 0660); E.Chk(e) {
  44  		// panic(e)
  45  	}
  46  }
  47  
  48  var configBase = `package config
  49  
  50  `+`//go:generate go run ./genopts/.
  51  
  52  import (
  53  	"github.com/p9c/p9/pkg/opts/binary"
  54  	"github.com/p9c/p9/pkg/opts/cmds"
  55  	"github.com/p9c/p9/pkg/opts/duration"
  56  	"github.com/p9c/p9/pkg/opts/float"
  57  	"github.com/p9c/p9/pkg/opts/integer"
  58  	"github.com/p9c/p9/pkg/opts/list"
  59  	"github.com/p9c/p9/pkg/opts/opt"
  60  	"github.com/p9c/p9/pkg/opts/text"
  61  )
  62  
  63  // Config defines the configuration items used by pod along with the various components included in the suite
  64  type Config struct {
  65  	// ShowAll is a flag to make the json encoder explicitly define all fields and not just the ones different to the
  66  	// defaults
  67  	ShowAll bool
  68  	// Map is the same data but addressible using its name as found inside the various configuration types, the key is
  69  	// converted to lower case for CLI args
  70  	Map            map[string]opt.Option
  71  	Commands       cmds.Commands
  72  	RunningCommand cmds.Command
  73  	ExtraArgs []string
  74  	FoundArgs []string
  75  %s}
  76  `
  77