2017-09-03 19:19:28 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
import (
|
2021-01-04 00:30:22 -05:00
|
|
|
"bufio"
|
2017-09-03 19:19:28 -04:00
|
|
|
"fmt"
|
|
|
|
|
"os"
|
2020-08-30 13:25:06 -04:00
|
|
|
"strings"
|
2017-09-03 19:19:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// LogFail is the failure log formatter
|
|
|
|
|
// prints text to stderr and exits with status 1
|
|
|
|
|
func LogFail(text string) {
|
2020-11-28 03:32:11 -05:00
|
|
|
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", text))
|
2017-09-03 19:19:28 -04:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 14:14:37 -04:00
|
|
|
// LogFailQuiet is the failure log formatter (with quiet option)
|
|
|
|
|
// prints text to stderr and exits with status 1
|
|
|
|
|
func LogFailQuiet(text string) {
|
|
|
|
|
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
|
2020-11-28 03:32:11 -05:00
|
|
|
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", text))
|
2020-03-10 14:14:37 -04:00
|
|
|
}
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Log is the log formatter
|
|
|
|
|
func Log(text string) {
|
|
|
|
|
fmt.Println(text)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LogQuiet is the log formatter (with quiet option)
|
|
|
|
|
func LogQuiet(text string) {
|
|
|
|
|
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
|
|
|
|
|
fmt.Println(text)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 19:19:28 -04:00
|
|
|
// LogInfo1 is the info1 header formatter
|
|
|
|
|
func LogInfo1(text string) {
|
2020-02-22 04:32:51 -05:00
|
|
|
fmt.Println(fmt.Sprintf("-----> %s", text))
|
2017-09-03 19:19:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LogInfo1Quiet is the info1 header formatter (with quiet option)
|
|
|
|
|
func LogInfo1Quiet(text string) {
|
2019-01-21 17:09:28 -05:00
|
|
|
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
|
2017-09-03 19:19:28 -04:00
|
|
|
LogInfo1(text)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LogInfo2 is the info2 header formatter
|
|
|
|
|
func LogInfo2(text string) {
|
2020-02-22 04:32:51 -05:00
|
|
|
fmt.Println(fmt.Sprintf("=====> %s", text))
|
2017-09-03 19:19:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LogInfo2Quiet is the info2 header formatter (with quiet option)
|
|
|
|
|
func LogInfo2Quiet(text string) {
|
2017-10-04 00:48:02 -04:00
|
|
|
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
|
2017-09-03 19:19:28 -04:00
|
|
|
LogInfo2(text)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 20:36:09 -04:00
|
|
|
// LogVerbose is the verbose log formatter
|
|
|
|
|
// prints indented text to stdout
|
2017-09-03 20:05:15 -04:00
|
|
|
func LogVerbose(text string) {
|
2020-02-22 04:32:51 -05:00
|
|
|
fmt.Println(fmt.Sprintf(" %s", text))
|
2017-09-03 20:05:15 -04:00
|
|
|
}
|
|
|
|
|
|
2017-09-03 20:36:09 -04:00
|
|
|
// LogVerboseQuiet is the verbose log formatter
|
|
|
|
|
// prints indented text to stdout (with quiet option)
|
2017-09-03 20:05:15 -04:00
|
|
|
func LogVerboseQuiet(text string) {
|
2019-01-21 17:09:28 -05:00
|
|
|
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
|
2017-09-03 20:05:15 -04:00
|
|
|
LogVerbose(text)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 13:25:06 -04:00
|
|
|
// LogVerboseQuietContainerLogs is the verbose log formatter for container logs
|
|
|
|
|
func LogVerboseQuietContainerLogs(containerID string) {
|
|
|
|
|
sc := NewShellCmdWithArgs(DockerBin(), "container", "logs", containerID)
|
|
|
|
|
sc.ShowOutput = false
|
|
|
|
|
b, err := sc.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
LogExclaim(fmt.Sprintf("Failed to fetch container logs: %s", containerID))
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 15:01:14 -04:00
|
|
|
output := strings.TrimSpace(string(b))
|
|
|
|
|
if len(output) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, line := range strings.Split(output, "\n") {
|
2020-08-31 02:05:21 -04:00
|
|
|
if line != "" {
|
|
|
|
|
LogVerboseQuiet(line)
|
|
|
|
|
}
|
2020-08-30 13:25:06 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 00:30:22 -05:00
|
|
|
// LogVerboseQuietContainerLogs is the verbose log formatter for container logs
|
|
|
|
|
func LogVerboseQuietContainerLogsTail(containerID string) {
|
|
|
|
|
sc := NewShellCmdWithArgs(DockerBin(), "container", "logs", containerID, "--follow", "--tail", "10")
|
|
|
|
|
stdout, _ := sc.Command.StdoutPipe()
|
|
|
|
|
sc.Command.Start()
|
|
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
m := scanner.Text()
|
|
|
|
|
fmt.Println(m)
|
|
|
|
|
}
|
|
|
|
|
sc.Command.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 19:19:28 -04:00
|
|
|
// LogWarn is the warning log formatter
|
|
|
|
|
func LogWarn(text string) {
|
|
|
|
|
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", text))
|
|
|
|
|
}
|
2020-02-08 15:56:45 -05:00
|
|
|
|
2020-07-17 19:46:02 -04:00
|
|
|
// LogExclaim is the log exclaim formatter
|
|
|
|
|
func LogExclaim(text string) {
|
|
|
|
|
fmt.Println(fmt.Sprintf(" ! %s", text))
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-08 15:56:45 -05:00
|
|
|
// LogStderr is the stderr log formatter
|
|
|
|
|
func LogStderr(text string) {
|
|
|
|
|
fmt.Fprintln(os.Stderr, text)
|
|
|
|
|
}
|
2020-08-30 13:38:38 -04:00
|
|
|
|
|
|
|
|
// LogDebug is the debug log formatter
|
|
|
|
|
func LogDebug(text string) {
|
2021-01-02 06:11:51 -05:00
|
|
|
if os.Getenv("DOKKU_TRACE") == "1" {
|
|
|
|
|
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ? %s", strings.TrimPrefix(text, " ? ")))
|
2020-08-30 13:38:38 -04:00
|
|
|
}
|
|
|
|
|
}
|