示例#1
0
文件: flock_proc.go 项目: gosuper/run
// This program is run by flock_test.go to verify the lock effect.
func main() {
	path := "/tmp/flock_test.lock"
	if os.Args[1] == "1" {
		time.Sleep(time.Millisecond * 100)
	}
	if err := flock.Flock(path); err != nil {
		panic(err)
	}
	if os.Args[1] == "0" {
		time.Sleep(time.Millisecond * 200)
		if err := flock.Funlock(path); err != nil {
			panic(err)
		}
	}
}
示例#2
0
文件: run.go 项目: gosuper/run
// Main function of the command run.
func main() {
	mask := syscall.Umask(0)
	defer syscall.Umask(mask)
	utils.SetConfigPath()
	utils.SetDataDir()
	initialize()

	// If run is not installed.
	if utils.IsRunInstalled() == false {
		utils.LogError("Run is not installed yet. You need to 'run --init' as root.\n")
		os.Exit(1)
	}

	// Show help message if no parameter given.
	if len(os.Args) == 1 {
		help()
		return
	}

	// Parse configuration and runtime options.
	config, err := utils.NewConfig()
	if err != nil {
		utils.ExitError(err)
	}
	options, err := utils.NewOptions(config)
	if err != nil {
		utils.ExitError(err)
	}

	// If print help message.
	if options.Help {
		help()
		return
	}

	// If output version information.
	if options.Version {
		version, err := ioutil.ReadFile(utils.DATA_DIR + "/VERSION")
		if err != nil {
			utils.ExitError(err)
		}
		utils.LogInfo("Run version %s\n", version)
		return
	}

	// If clean out scripts.
	if options.Clean {
		utils.LogInfo("Do you want to clear out the script cache? [Y/n] ")
		var answer string
		fmt.Scanln(&answer)
		if answer == "Y" || answer == "y" {
			// rm -rf $DATA_DIR/* will remove VERSION. Use $DATA_DIR/*/ instead.
			utils.Exec([]string{"sh", "-x", "-c", "rm -rf " + utils.DATA_DIR + "/*/"})
		}
		return
	}

	// If not script given.
	if options.Fields == nil {
		utils.LogError("The script to run is not specified\n")
		os.Exit(1)
	}

	// Ensure the cache directory has been created.
	cacheID := options.CacheID
	cacheDir := utils.DATA_DIR + "/" + options.Scope + "/" + cacheID
	err = os.MkdirAll(cacheDir, 0777)
	if err != nil {
		utils.ExitError(err)
	}

	// Lock the script.
	lockPath := cacheDir + ".lock"
	err = flock.Flock(lockPath)
	if err != nil {
		utils.LogError("%s: %v\n", lockPath, err)
		os.Exit(1)
	}

	// Download the script.
	scriptPath := cacheDir + "/" + options.Script
	_, err = os.Stat(scriptPath)
	if os.IsNotExist(err) || options.Update {
		err = utils.Fetch(options.URL, scriptPath)
		if err != nil {
			utils.ExitError(err)
		}
	}

	// If view the script.
	if options.View {
		flock.Funlock(lockPath)
		utils.Exec([]string{"cat", scriptPath})
	}

	// Run the script.
	flock.Funlock(lockPath)
	if options.Interpreter == "" {
		utils.Exec(append([]string{scriptPath}, options.Args...))
	} else {
		utils.Exec(append([]string{options.Interpreter, scriptPath}, options.Args...))
	}
}