Example #1
0
func checkHook(out chan []byte, fix bool, name string, hookPath string, hookURL string) {
	var buffer bytes.Buffer
	defer func() {
		out <- buffer.Bytes()
	}()

	found := util.FileExist(hookPath)
	if found {
		fmt.Fprintf(&buffer, colour.FgGreenf+"\tFound %s hook\n", "OK", name)
		// TODO: Verify that this is indeed the hook. Either by grepping for {name} or
		// making diff to content of hookURL. This would also work as a "check for updates" function.
	} else {
		fmt.Fprintf(&buffer, colour.FgRedf+"\t%s hook not installed [%s]\n", "FAIL", name, hookPath)
		if fix {
			fmt.Fprintf(&buffer, colour.FgBluef+"\tInstalling %s hook.\n", "FIX", name)
			err := download(hookPath, hookURL)
			if err != nil {
				panic(fmt.Sprintf("Unable to download %s hook.", name))
			}
			fmt.Fprintf(&buffer, colour.FgGreenf+"\t%s hook downloaded and installed.\n", "SUCCESS", name)
		}
	}
}
Example #2
0
// CheckSshConfig checks, an optionally, fixes the SSH config related to Git.
func CheckSshConfig(fix bool, cmux *sync.Mutex) {

	// Output buffer until we are forced to gain access to console.
	var buffer bytes.Buffer
	defer func() {
		// Get access to console
		cmux.Lock()
		defer cmux.Unlock()
		fmt.Print(buffer.String())
	}()

	fmt.Fprintln(&buffer)
	fmt.Fprintf(&buffer, colour.Boldf+"\n", "Checking global SSH configuration")

	my, err := user.Current()
	if err != nil {
		panic("Unable to get current user from OS.")
	}

	configDir := my.HomeDir + "/.ssh"
	configFile := configDir + "/config"

	// Open config dir/file or create it if necessary.
	dirFound := util.FileExist(configDir)
	if dirFound {
		fmt.Fprintf(&buffer, colour.FgGreenf+"\tSSH configuration directory found.\n", "OK")
	} else {
		fmt.Fprintf(&buffer, colour.FgRedf+"\tSSH configuration directory not found [%s].\n", "FAIL", configDir)
		if fix {
			fmt.Fprintf(&buffer, colour.FgBluef+"\tCreating directory.\n", "FIX")
			err := os.Mkdir(configDir, 0700)
			if err != nil {
				panic("Could not create directory.")
			}
		}
	}

	fileFound := util.FileExist(configFile)
	if fileFound {
		fmt.Fprintf(&buffer, colour.FgGreenf+"\tSSH configuration file found.\n", "OK")
	} else {
		fmt.Fprintf(&buffer, colour.FgRedf+"\tSSH configuration file not found.\n", "FAIL")
		if fix {
			fmt.Fprintf(&buffer, colour.FgBluef+"\tCreating configuration file.\n", "FIX")
			file, err := os.OpenFile(configFile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
			if err != nil {
				panic("Could not create configuration file.")
			}
			file.Close()
			fmt.Fprintf(&buffer, colour.FgGreenf+"\tSSH configuration file created.\n", "SUCCESS")
		}
	}

	// Grep for "Host gerrit"
	found, err := util.Grep("Host gerrit", configFile)
	if err != nil {
		panic("Unable to read SSH configuration.")
	}
	if found {
		fmt.Fprintf(&buffer, colour.FgGreenf+"\tHost 'gerrit' configured.\n", "OK")
	} else {
		fmt.Fprintf(&buffer, colour.FgRedf+"\tHost 'gerrit' not configured.\n", "FAIL")

		if fix {
			fmt.Fprintf(&buffer, colour.FgBluef+"\tAdding 'Host gerrit'.\n", "FIX")
			file, err := os.OpenFile(configFile, os.O_APPEND|os.O_WRONLY, 0600)
			if err != nil {
				panic("Unable to write open configuration file for writing.")
			}
			defer file.Close()

			_, err = file.WriteString("Host gerrit\n")
			if err != nil {
				panic("Unable to write to configuration file.")
			}
			fmt.Fprintf(&buffer, colour.FgGreenf+"\tHost written to configuration file.\n", "SUCCESS")

			fmt.Fprintf(&buffer, colour.FgBluef+"\tAdding 'Port 29418'.\n", "FIX")
			_, err = file.WriteString("\tPort 29418\n")
			if err != nil {
				panic("Unable to write to configuration file.")
			}
			fmt.Fprintf(&buffer, colour.FgGreenf+"\tPort written to configuration file.\n", "SUCCESS")
		}
	}
}