1// Copyright 2015 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package raft
16
17import (
18	"fmt"
19	"io"
20	"io/ioutil"
21	"os"
22	"os/exec"
23	"strings"
24)
25
26func diffu(a, b string) string {
27	if a == b {
28		return ""
29	}
30	aname, bname := mustTemp("base", a), mustTemp("other", b)
31	defer os.Remove(aname)
32	defer os.Remove(bname)
33	cmd := exec.Command("diff", "-u", aname, bname)
34	buf, err := cmd.CombinedOutput()
35	if err != nil {
36		if _, ok := err.(*exec.ExitError); ok {
37			// do nothing
38			return string(buf)
39		}
40		panic(err)
41	}
42	return string(buf)
43}
44
45func mustTemp(pre, body string) string {
46	f, err := ioutil.TempFile("", pre)
47	if err != nil {
48		panic(err)
49	}
50	_, err = io.Copy(f, strings.NewReader(body))
51	if err != nil {
52		panic(err)
53	}
54	f.Close()
55	return f.Name()
56}
57
58func ltoa(l *raftLog) string {
59	s := fmt.Sprintf("committed: %d\n", l.committed)
60	s += fmt.Sprintf("applied:  %d\n", l.applied)
61	for i, e := range l.allEntries() {
62		s += fmt.Sprintf("#%d: %+v\n", i, e)
63	}
64	return s
65}
66