コード例 #1
0
ファイル: file.go プロジェクト: UnnoTed/fileb0x
// FromArg gets the json/yaml file from args
func (f *File) FromArg(read bool) error {
	// (length - 1)
	arg := os.Args[len(os.Args)-1:][0]

	// get extension
	ext := path.Ext(arg)
	if len(ext) > 1 {
		ext = ext[1:] // remove dot
	}

	// when json/yaml file isn't found on last arg
	// it searches for a ".json" or ".yaml" string in all args
	if ext != "json" && ext != "yaml" {
		// loop through args
		for _, a := range os.Args {
			// get extension
			ext := path.Ext(a)

			// check for valid extensions
			if ext == ".json" || ext == ".yaml" {
				f.Mode = ext[1:] // remove dot
				ext = f.Mode
				arg = a
				break
			}
		}
	} else {
		f.Mode = ext
	}

	// check if extension is json or yaml
	// then get it's absolute path
	if ext == "json" || ext == "yaml" {
		abs := filepath.IsAbs(arg)
		if !abs {
			dir, err := utils.GetCurrentDir()
			if err != nil {
				return err
			}

			arg = filepath.Clean(dir + "/" + arg)
		}

		f.FilePath = arg

		// so we can test without reading a file
		if read {
			if !utils.Exists(f.FilePath) {
				return errors.New("Error: I Can't find the config file at [" + f.FilePath + "]")
			}
		}
	} else {
		return errors.New("Error: You must specify a json or yaml file")
	}

	return nil
}
コード例 #2
0
ファイル: config_test.go プロジェクト: UnnoTed/fileb0x
func TestConfigClean(t *testing.T) {
	var err error
	cfg := new(Config)
	cfg.Dest = "./clean_test123456789/"
	cfg.Clean = true

	err = os.Mkdir(cfg.Dest, 0777)
	assert.NoError(t, err)

	var files = []string{
		"a.go",
		"b.go",
		"c.go",
		"d.go",
		"e.go",
		"f.go",
		"g.go",
		"h.go",
	}

	// write files into the folder so it can be cleaned by Config.Defaults()
	nothing := []byte("nothing")
	for _, file := range files {
		err = ioutil.WriteFile(cfg.Dest+"b0xfile_"+file, nothing, 0777)
		assert.NoError(t, err)
		assert.True(t, utils.Exists(cfg.Dest+"b0xfile_"+file))
	}

	// clean
	err = cfg.Defaults()
	assert.NoError(t, err)

	// verify that the files were deleTed
	for _, file := range files {
		assert.False(t, utils.Exists(cfg.Dest+"b0xfile_"+file))
	}

	// delete dest folder
	os.RemoveAll(cfg.Dest)
	assert.False(t, utils.Exists(cfg.Dest))
}
コード例 #3
0
ファイル: file.go プロジェクト: UnnoTed/fileb0x
// Load the json/yaml file that was specified from args
// and transform it into a config struct
func (f *File) Load() (*Config, error) {
	var err error
	if !utils.Exists(f.FilePath) {
		return nil, errors.New("Error: I Can't find the config file at [" + f.FilePath + "]")
	}

	// read file
	f.Data, err = ioutil.ReadFile(f.FilePath)
	if err != nil {
		return nil, err
	}

	// parse file
	return f.Parse()
}
コード例 #4
0
ファイル: main.go プロジェクト: UnnoTed/fileb0x
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	// create config and try to get b0x file from args
	f := new(config.File)
	err = f.FromArg(true)
	if err != nil {
		log.Fatal(err)
	}

	// load b0x file's config
	cfg, err = f.Load()
	if err != nil {
		log.Fatal(err)
	}

	err = cfg.Defaults()
	if err != nil {
		log.Fatal(err)
	}

	// creates a config that can be inserTed into custom
	// without causing a import cycle
	sharedConfig := new(custom.SharedConfig)
	sharedConfig.Output = cfg.Output
	sharedConfig.Compression = compression.NewGzip()
	sharedConfig.Compression.Options = cfg.Compression

	// loop through b0x's [custom] objects
	for _, c := range cfg.Custom {
		err = c.Parse(&files, &dirs, sharedConfig)
		if err != nil {
			log.Fatal(err)
		}
	}

	// create files template and exec it
	t := new(template.Template)
	t.Set("files")
	t.Variables = struct {
		Pkg         string
		Files       map[string]*file.File
		Spread      bool
		DirList     []string
		Compression *compression.Options
		Debug       bool
	}{
		Pkg:         cfg.Pkg,
		Files:       files,
		Spread:      cfg.Spread,
		DirList:     dirs.Clean(),
		Compression: cfg.Compression,
		Debug:       cfg.Debug,
	}
	tmpl, err := t.Exec()
	if err != nil {
		log.Fatal(err)
	}

	// create dest folder when it doesn't exists
	if !utils.Exists(cfg.Dest) {
		err = os.MkdirAll(cfg.Dest, 0777)
		if err != nil {
			log.Fatal(err)
		}
	}

	// gofmt
	if cfg.Fmt {
		tmpl, err = format.Source(tmpl)
		if err != nil {
			log.Fatal(err)
		}
	}

	// write final execuTed template into the destination file
	err = ioutil.WriteFile(cfg.Dest+cfg.Output, tmpl, 0777)
	if err != nil {
		log.Fatal(err)
	}

	// write spread files
	if cfg.Spread {
		a := strings.Split(path.Dir(cfg.Dest), "/")
		dirName := a[len(a)-1:][0]

		for _, f := range files {
			a := strings.Split(path.Dir(f.Path), "/")
			fileDirName := a[len(a)-1:][0]

			if dirName == fileDirName {
				continue
			}

			// transform / to _ and some other chars...
			customName := "b0xfile_" + utils.FixName(f.Path) + ".go"

			// creates file template and exec it
			t := new(template.Template)
			t.Set("file")
			t.Variables = struct {
				Pkg         string
				Path        string
				Name        string
				Dir         [][]string
				Data        string
				Compression *compression.Options
			}{
				Pkg:         cfg.Pkg,
				Path:        f.Path,
				Name:        f.Name,
				Dir:         dirs.List,
				Data:        f.Data,
				Compression: cfg.Compression,
			}
			tmpl, err := t.Exec()
			if err != nil {
				log.Fatal(err)
			}

			// gofmt
			if cfg.Fmt {
				tmpl, err = format.Source(tmpl)
				if err != nil {
					log.Fatal(err)
				}
			}

			// write final execuTed template into the destination file
			err = ioutil.WriteFile(cfg.Dest+customName, tmpl, 0777)
			if err != nil {
				log.Fatal(err)
			}
		}
	}

	// success
	log.Println("fileb0x:", cfg.Dest+cfg.Output, "written!")
}