当前位置: 首页>>代码示例>>Golang>>正文


Golang environments.Environment类代码示例

本文整理汇总了Golang中github.com/9seconds/ah/app/environments.Environment的典型用法代码示例。如果您正苦于以下问题:Golang Environment类的具体用法?Golang Environment怎么用?Golang Environment使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Environment类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ListBookmarks

// ListBookmarks prints the list of bookmarks with their content
func ListBookmarks(env *environments.Environment) {
	bookmarksFileInfos, err := env.GetBookmarksFileInfos()
	if err != nil {
		utils.Logger.Panic(err)
	}

	maxLength := 1
	for _, fileInfo := range bookmarksFileInfos {
		name := fileInfo.Name()
		if len(name) > maxLength {
			maxLength = len(name)
		}
	}

	template := "%-" + strconv.Itoa(maxLength) + "s    %s\n"
	utils.Logger.WithFields(logrus.Fields{
		"template": template,
	}).Info("Calculated template to print")

	for _, fileInfo := range bookmarksFileInfos {
		fileName := fileInfo.Name()

		content, err := ioutil.ReadFile(env.GetBookmarkFileName(fileName))
		if err != nil {
			utils.Logger.WithFields(logrus.Fields{
				"filename": fileName,
				"error":    err,
			}).Warn("Cannot read a content of the file so skip")
			continue
		}

		fmt.Printf(template, fileName, string(content))
	}
}
开发者ID:9seconds,项目名称:ah,代码行数:35,代码来源:list_bookmarks.go

示例2: processHistories

func processHistories(env *environments.Environment) (resultChan chan bool, consumeChan chan *HistoryEntry) {
	resultChan = make(chan bool, 1)
	consumeChan = make(chan *HistoryEntry, historyEventsCapacity)

	go func() {
		entries := make(map[string]bool)

		files, err := env.GetTracesFileInfos()
		if err != nil {
			utils.Logger.WithFields(logrus.Fields{
				"error": err,
			}).Warn("Error on traces directory listing")
			resultChan <- true
			return
		}

		for _, file := range files {
			entries[file.Name()] = true
		}
		utils.Logger.WithField("filenames", entries).Info("Parsed filenames")

		for entry := range consumeChan {
			if _, found := entries[entry.GetTraceName()]; found {
				entry.hasHistory = true
			}
		}
		resultChan <- true
	}()

	return
}
开发者ID:9seconds,项目名称:ah,代码行数:31,代码来源:history_processor.go

示例3: ListTrace

// ListTrace implements l command (list trace).
func ListTrace(argument string, env *environments.Environment) {
	number, err := strconv.Atoi(argument)
	if err != nil || number < 0 {
		utils.Logger.Panicf("Cannot convert argument to a command number: %s", argument)
	}

	commands, err := historyentries.GetCommands(historyentries.GetCommandsPrecise, nil, env, number)
	if err != nil {
		utils.Logger.Panic(err)
	}
	command := commands.Result().(historyentries.HistoryEntry)
	hashFilename := command.GetTraceName()
	filename := env.GetTraceFileName(hashFilename)
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		utils.Logger.Panicf("Output for %s is not exist", argument)
	}

	file := utils.Open(filename)
	defer file.Close()
	ungzippedFile, err := gzip.NewReader(file)
	if err != nil {
		utils.Logger.Panic(err)
	}
	defer ungzippedFile.Close()

	scanner := bufio.NewScanner(ungzippedFile)
	for scanner.Scan() {
		os.Stdout.WriteString(scanner.Text())
		os.Stdout.WriteString("\n")
	}

	if err := scanner.Err(); err != nil {
		utils.Logger.Panic(err)
	}
}
开发者ID:9seconds,项目名称:ah,代码行数:36,代码来源:list_trace.go

示例4: ExecuteBookmark

// ExecuteBookmark executes command by its bookmark name.
func ExecuteBookmark(name string, interactive bool, pseudoTTY bool, env *environments.Environment) {
	content, err := ioutil.ReadFile(env.GetBookmarkFileName(name))
	if err != nil {
		utils.Logger.Panic("Unknown bookmark")
	}

	execute(string(content), env.Shell, interactive, pseudoTTY)
}
开发者ID:9seconds,项目名称:ah,代码行数:9,代码来源:execute.go

示例5: ToString

// ToString converts history entry to the string representation according to the environment setting.
func (he HistoryEntry) ToString(env *environments.Environment) string {
	timestamp := ""
	if formattedTimestamp := env.FormatTimeStamp(he.timestamp); formattedTimestamp != "" {
		timestamp = "  (" + formattedTimestamp + ")"
	}

	history := markHasNoHistory
	if he.hasHistory {
		history = markHasHistory
	}

	return fmt.Sprintf("!%-5d%s %c  %s", he.number, timestamp, history, he.command)
}
开发者ID:9seconds,项目名称:ah,代码行数:14,代码来源:history_entry.go

示例6: GetCommands

// GetCommands returns a keeper for the commands based on given mode and regular expression.
// varargs is the auxiliary list of numbers which makes sense in the context of GetCommandsMode setting
// only.
func GetCommands(mode GetCommandsMode, filter *utils.Regexp, env *environments.Environment, varargs ...int) (commands Keeper, err error) {
	keeper := getKeeper(mode, varargs...)
	resultChan, consumeChan := processHistories(env)
	parser := getParser(env)

	histFileName, err := env.GetHistFileName()
	if err != nil {
		return
	}

	file := utils.Open(histFileName)
	defer file.Close()
	scanner := bufio.NewScanner(file)

	commands, err = parser(keeper, scanner, filter, consumeChan)
	if err == nil {
		<-resultChan
		return commands, nil
	}
	return
}
开发者ID:9seconds,项目名称:ah,代码行数:24,代码来源:get_commands.go

示例7: Tee

// Tee implements t (trace, tee) command.
func Tee(input string, interactive bool, pseudoTTY bool, env *environments.Environment) {
	output, err := ioutil.TempFile(env.TmpDir, "ah")
	if err != nil {
		utils.Logger.Panic("Cannot create temporary file")
	}

	bufferedOutput := bufio.NewWriter(output)
	gzippedWrapper := utils.NewSynchronizedWriter(gzip.NewWriter(bufferedOutput))
	combinedStdout := io.MultiWriter(os.Stdout, gzippedWrapper)
	combinedStderr := io.MultiWriter(os.Stderr, gzippedWrapper)

	var commandError *exec.ExitError
	defer func() {
		// defer here because command may cause a panic but we do not want to lose any output
		gzippedWrapper.Close()
		bufferedOutput.Flush()
		output.Close()

		if hash, err := getPreciseHash(input, env); err == nil {
			err = os.Rename(output.Name(), env.GetTraceFileName(hash))
			if err != nil {
				utils.Logger.Errorf("Cannot save trace: %v. Get it here: %s", err, output.Name())
			} else {
				os.Remove(output.Name())
			}
		} else {
			utils.Logger.Errorf("Error occured on fetching command number: %v", err)
		}

		if commandError != nil {
			os.Exit(utils.GetStatusCode(commandError))
		}
	}()

	commandError = utils.Exec(input,
		string(env.Shell), interactive, pseudoTTY,
		os.Stdin, combinedStdout, combinedStderr)
}
开发者ID:9seconds,项目名称:ah,代码行数:39,代码来源:tee.go

示例8: Bookmark

// Bookmark implements "b" (bookmark) command.
func Bookmark(commandNumber int, bookmarkAs string, env *environments.Environment) {
	if commandNumber < 0 {
		utils.Logger.Panic("Command number should be >= 0")
	}

	commandsKeeper, err := historyentries.GetCommands(historyentries.GetCommandsAll, nil, env)
	if err != nil {
		utils.Logger.Panic(err)
	}
	commands := commandsKeeper.Result().([]historyentries.HistoryEntry)
	if len(commands) <= commandNumber {
		utils.Logger.Panic("Command number does not exist")
	}
	command := commands[commandNumber-1]

	filename := env.GetBookmarkFileName(bookmarkAs)
	file, err := os.Create(filename)
	if err != nil {
		utils.Logger.Panicf("Cannot create bookmark %s: %v", filename, err)
	}
	defer file.Close()

	file.WriteString(command.GetCommand())
}
开发者ID:9seconds,项目名称:ah,代码行数:25,代码来源:bookmark.go

示例9: RemoveBookmarks

// RemoveBookmarks removes the list of bookmarks from the storage.
func RemoveBookmarks(bookmarks []string, env *environments.Environment) {
	for _, bookmark := range bookmarks {
		utils.RemoveWithLogging(env.GetBookmarkFileName(bookmark))
	}
}
开发者ID:9seconds,项目名称:ah,代码行数:6,代码来源:remove_bookmarks.go

示例10: main

func main() {
	defer func() {
		if exc := recover(); exc != nil {
			utils.Logger.Fatal(exc)
		}
	}()

	arguments, err := docopt.Parse(docoptOptions, nil, true, version, false)
	if err != nil {
		panic(err)
	}

	if arguments["--debug"].(bool) {
		utils.EnableLogging()
	} else {
		utils.DisableLogging()
	}
	utils.Logger.WithField("arguments", arguments).Info("Parsed arguments")

	defaultEnv := environments.MakeDefaultEnvironment()
	cmdLineEnv := new(environments.Environment)
	configEnv, err := defaultEnv.ReadFromConfig()
	if err != nil {
		utils.Logger.WithField("error", err).Warn("Cannot read config file")
	}

	argShell := arguments["--shell"]
	if argShell != nil {
		cmdLineEnv.Shell = argShell.(string)
	}

	argHistFile := arguments["--histfile"]
	if argHistFile != nil {
		cmdLineEnv.HistFile = argHistFile.(string)
	}

	argHistTimeFormat := arguments["--histtimeformat"]
	if argHistTimeFormat != nil {
		cmdLineEnv.HistTimeFormat = argHistTimeFormat.(string)
	}

	argAppDir := arguments["--appdir"]
	if argAppDir != nil {
		cmdLineEnv.AppDir = argAppDir.(string)
	}

	argTmpDir := arguments["--tmpdir"]
	if argTmpDir != nil {
		cmdLineEnv.TmpDir = argTmpDir.(string)
	}

	utils.Logger.WithFields(logrus.Fields{
		"default":    defaultEnv,
		"config":     configEnv,
		"cmdLineEnv": cmdLineEnv,
	}).Debug("Environments")

	env := environments.MergeEnvironments(defaultEnv, configEnv, cmdLineEnv)
	utils.Logger.WithField("result env", env).Debug("Ready to start")

	utils.Logger.WithFields(logrus.Fields{
		"error": os.MkdirAll(env.TracesDir, 0777),
	}).Info("Create traces dir")
	utils.Logger.WithFields(logrus.Fields{
		"error": os.MkdirAll(env.BookmarksDir, 0777),
	}).Info("Create bookmarks dir")
	utils.Logger.WithFields(logrus.Fields{
		"error": os.MkdirAll(env.TmpDir, 0777),
	}).Info("Create create temporary dir")

	var exec executor
	switch {
	case arguments["t"].(bool):
		utils.Logger.Info("Execute command 'tee'")
		exec = executeTee
	case arguments["s"].(bool):
		utils.Logger.Info("Execute command 'show'")
		exec = executeShow
	case arguments["l"].(bool):
		utils.Logger.Info("Execute command 'listTrace'")
		exec = executeListTrace
	case arguments["b"].(bool):
		utils.Logger.Info("Execute command 'listTrace'")
		exec = executeListTrace
	case arguments["e"].(bool):
		utils.Logger.Info("Execute command 'execute'")
		exec = executeExec
	case arguments["lb"].(bool):
		utils.Logger.Info("Execute command 'listBookmarks'")
		exec = executeListBookmarks
	case arguments["rb"].(bool):
		utils.Logger.Info("Execute command 'removeBookmarks'")
		exec = executeRemoveBookmarks
	case arguments["gt"].(bool) || arguments["gb"].(bool):
		utils.Logger.Info("Execute command 'gc'")
		exec = executeGC
	case arguments["al"].(bool):
		utils.Logger.Info("Execute command 'al'")
		exec = executeAl
	case arguments["ad"].(bool):
//.........这里部分代码省略.........
开发者ID:9seconds,项目名称:ah,代码行数:101,代码来源:main.go

示例11: GetFormattedTime

// GetFormattedTime returns formatted time stamp of the history entry.
func (he HistoryEntry) GetFormattedTime(env *environments.Environment) string {
	return env.FormatTimeStamp(he.timestamp)
}
开发者ID:9seconds,项目名称:ah,代码行数:4,代码来源:history_entry.go


注:本文中的github.com/9seconds/ah/app/environments.Environment类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。