コード例 #1
0
ファイル: run.go プロジェクト: terminiter/earthquake
// depends on initConfig()
func (this *runner) initStorage() error {
	var err error
	this.storage, err = historystorage.New(this.config.GetString("storageType"), this.storageDirPath)
	if err != nil {
		return err
	}
	this.storage.Init()
	this.workingDirPath = this.storage.CreateNewWorkingDir()
	this.materialsDirPath = path.Join(this.storageDirPath, storageMaterialsPath)
	return nil
}
コード例 #2
0
ファイル: init.go プロジェクト: terminiter/earthquake
// FIXME: refactor
func _init(args []string) int {
	if err := initFlagset.Parse(args); err != nil {
		fmt.Printf("%s", err.Error())
		return 1
	}

	if initFlagset.NArg() != 3 {
		fmt.Printf("specify <config file path> <materials dir path> <storage dir path>\n")
		return 1
	}

	confPath := initFlagset.Arg(0)
	materials := initFlagset.Arg(1)
	storagePath := initFlagset.Arg(2)

	cfi, err := os.Stat(confPath)
	if err != nil {
		fmt.Printf("failed to stat path: %s (%s)\n", confPath, err)
		return 1
	}

	if !cfi.Mode().IsRegular() {
		fmt.Printf("config file (%s) must be a regular file\n", confPath)
		return 1
	}
	if _initFlags.Force {
		// avoid catastrophe caused by a typo
		wellKnownDirs := map[string]bool{
			".": true, "..": true, "/": true, "/home": true, "/tmp": true, "/dummyWellKnownDir": true}
		if wellKnownDirs[storagePath] {
			fmt.Printf("storage dir(%s) typo?\n", storagePath)
			return 1
		}
		if err = os.RemoveAll(storagePath); err != nil {
			// NOTE: if storagePath does not exist, RemoveAll returns nil
			fmt.Printf("could not remove %s (%s)\n", storagePath, err)
			return 1
		}
		if err = os.Mkdir(storagePath, 0777); err != nil {
			fmt.Printf("could not create %s (%s)\n", storagePath, err)
			return 1
		}
	}

	sfi, err := os.Stat(storagePath)
	if err != nil {
		fmt.Printf("failed to stat path: %s (%s)\n", storagePath, err)
		return 1
	}

	if !sfi.Mode().IsDir() {
		fmt.Printf("storagePath directory (%s) must be a directory\n", storagePath)
		return 1
	}

	dir, err := os.Open(storagePath)
	if err != nil {
		fmt.Printf("failed to open storagePath directory: %s (%s)\n", storagePath, err)
		return 1
	}

	fi, err := dir.Readdir(0)
	if err != nil {
		fmt.Printf("failed to read storagePath directory: %s (%s)\n", storagePath, err)
		return 1
	}

	if len(fi) != 0 {
		fmt.Printf("directory for earthquake storagePath (%s) must be empty\n", storagePath)
		return 1
	}

	cfg, err := config.NewFromFile(confPath)
	if err != nil {
		fmt.Printf("parsing config file (%s) failed: %s\n", confPath, err)
		return 1
	}

	if !strings.EqualFold(filepath.Ext(confPath), ".toml") {
		fmt.Printf("this version does not support non-TOML configs")
		return 1
	}
	err = copyFile(path.Join(storagePath, historystorage.StorageTOMLConfigPath), confPath, 0644)
	if err != nil {
		fmt.Printf("placing config file (%s) failed (%s)\n", confPath, err)
		return 1
	}

	materialsDir := path.Join(storagePath, storageMaterialsPath)
	err = os.Mkdir(materialsDir, 0777)
	if err != nil {
		fmt.Printf("creating a directory for materials (%s) failed (%s)\n",
			materialsDir, err)
		return 1
		// TODO: cleaning conf file
	}
	cmd.DefaultFactory.SetMaterialsDir(materialsDir)

	err = recursiveHardLink(materials, materialsDir)
	if err != nil {
		fmt.Printf("%s\n", err)
		return 1
	}

	storage, err := historystorage.New(cfg.GetString("storageType"), storagePath)
	if err != nil {
		fmt.Printf("%s\n", err)
		return 1
	}
	storage.CreateStorage()

	if cfg.GetString("init") != "" {
		initScriptPath := path.Join(materialsDir, cfg.GetString("init"))
		runCmd := cmd.DefaultFactory.CreateCmd(initScriptPath)
		if err = runCmd.Run(); err != nil {
			fmt.Printf("could not run %s (%s)\n", initScriptPath, err)
			return 1
		}
	}
	fmt.Printf("ok\n")
	return 0
}