Exemple #1
0
// Apply configures your system to use bashistdb:
// 1. It appends to your ~/.bashrc two lines to make your history timestamped
//    and your prompt send your commands to bashistdb.
// 2. It (optionally) adds timestamps to your current history file, so it can
//    be used with bashistdb. This step is also safe to run many times.
func Apply(write bool) error {
	// Setup bashrc for bashistdb
	bashrc := os.Getenv("HOME") + "/.bashrc"
	f, err := os.OpenFile(bashrc, os.O_APPEND|os.O_WRONLY, 0600)
	if err != nil {
		return errors.New("Could not open bashrc: " + err.Error())
	}
	defer f.Close()

	if _, err = f.WriteString(appendLines); err != nil {
		return errors.New("Could not write bashrc: " + err.Error())
	}
	log.Println("Updated " + bashrc + ", appended: \n" + appendLines)

	// Convert bash_history
	if write {
		bashHistory := os.Getenv("HOME") + "/.bash_history"
		historyIn, err := ioutil.ReadFile(bashHistory)
		if err != nil {
			return errors.New("Could not read bash_history: " + err.Error())
		}

		historyOut := timestamp.Convert(historyIn, 12)

		err = ioutil.WriteFile(bashHistory, historyOut, 0600)
		if err != nil {
			return errors.New("Could not write bash_history: " + err.Error())
		}
		log.Println("Updated " + bashHistory)
	}

	return nil
}
func main() {
	flag.Parse()

	historyIn, err := ioutil.ReadFile(*historyFile)
	if err != nil {
		log.Fatalln(err)
	}
	historyOut := timestamp.Convert(historyIn, *duration)

	if !*write {
		fmt.Println(string(historyOut))
	} else {
		err = ioutil.WriteFile(*historyFile, historyOut, 0600)

		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Println("History file converted.")
		}
	}
}