コード例 #1
0
ファイル: output.go プロジェクト: kaneda-t/rsyncdiff
func ContextOutput(diffTargets []string) (int, error) {
	tmp, err := ioutil.TempFile(os.TempDir(), "rsyncdiff")
	if err != nil {
		return 1, err
	}
	defer os.Remove(tmp.Name())

	for _, target := range diffTargets {
		diffInfo, err := NewDiffInfo(target)
		if err != nil {
			return 1, err
		}
		diff := difflib.ContextDiff{
			A:        diffInfo.A,
			B:        diffInfo.B,
			FromFile: diffInfo.FromFile,
			ToFile:   diffInfo.ToFile,
		}
		result, err := difflib.GetContextDiffString(diff)
		if err != nil {
			return 1, err
		}
		_, err = tmp.WriteString(result)
		if err != nil {
			return 1, err
		}
		tmp.Sync()
	}

	cmd := &Command{}
	cmd.Command = "cat"
	cmd.Options = []string{tmp.Name()}
	return cmd.Run()
}
コード例 #2
0
ファイル: command-format.go プロジェクト: fritzing/fzp
func formatFile(filepath string, runWrite, runDiff bool) error {
	format, _ := fzp.GetFormat(filepath)

	if format != fzp.FormatFzp {
		return errors.New("at the moment only fzp format supported")
	}

	fzpFile, fzpBytes, err := fzp.ReadFzp(filepath)
	if err != nil {
		fmt.Println(err)
		return err
	}

	formattedXML, err := fzpFile.ToXML()
	if err != nil {
		fmt.Println(err)
		return err
	}

	if runWrite {
		err := ioutil.WriteFile(filepath, formattedXML, 0755)
		if err != nil {
			fmt.Println("Error", err)
			os.Exit(127)
		}
		return nil
	}

	// diff
	if runDiff {
		diff := difflib.ContextDiff{
			A:        difflib.SplitLines(string(fzpBytes)),
			B:        difflib.SplitLines(string(formattedXML)),
			FromFile: filepath,
			ToFile:   "Current",
			Context:  3,
			Eol:      "\n",
		}
		result, _ := difflib.GetContextDiffString(diff)
		fmt.Printf(strings.Replace(result, "\t", " ", -1))
		return nil
	}

	fmt.Println(string(formattedXML))
	return nil
}