forked from konstructio/kubefirst
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b6b5e0
commit c625a5d
Showing
14 changed files
with
337 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright (C) 2021-2023, Kubefirst | ||
This program is licensed under MIT. | ||
See the LICENSE file for more details. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/kubefirst/kubefirst/internal/provisionLogs" | ||
"github.com/nxadm/tail" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// infoCmd represents the info command | ||
var logsCmd = &cobra.Command{ | ||
Use: "logs", | ||
Short: "kubefirst real time logs", | ||
Long: `kubefirst real time logs`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
provisionLogs.InitializeProvisionLogsTerminal() | ||
|
||
go func() { | ||
t, err := tail.TailFile(viper.GetString("k1-paths.log-file"), tail.Config{Follow: true, ReOpen: true}) | ||
if err != nil { | ||
fmt.Printf("Error tailing log file: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
for line := range t.Lines { | ||
provisionLogs.AddLog(line.Text) | ||
} | ||
}() | ||
|
||
provisionLogs.ProvisionLogs.Run() | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(logsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright (C) 2021-2023, Kubefirst | ||
This program is licensed under MIT. | ||
See the LICENSE file for more details. | ||
*/ | ||
package provisionLogs | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/muesli/termenv" | ||
) | ||
|
||
type Log struct { | ||
Level string `bson:"level" json:"level"` | ||
Time string `bson:"time" json:"time"` | ||
Message string `bson:"message" json:"message"` | ||
} | ||
|
||
var ( | ||
color = termenv.EnvColorProfile().Color | ||
infoStyle = termenv.Style{}.Foreground(color("27")).Styled | ||
errorStyle = termenv.Style{}.Foreground(color("196")).Styled | ||
timeStyle = termenv.Style{}.Foreground(color("245")).Bold().Styled | ||
textStyle = termenv.Style{}.Foreground(color("15")).Styled | ||
) | ||
|
||
func AddLog(logMsg string) { | ||
log := Log{} | ||
formatterMsg := "" | ||
|
||
err := json.Unmarshal([]byte(logMsg), &log) | ||
if err != nil { | ||
formatterMsg = textStyle(logMsg) | ||
} else { | ||
parsedTime, err := time.Parse(time.RFC3339, log.Time) | ||
if err != nil { | ||
fmt.Println("Error parsing date:", err) | ||
return | ||
} | ||
|
||
// Format the parsed time into the desired format | ||
formattedDateStr := parsedTime.Format("2006-01-02 15:04:05") | ||
|
||
timeLog := timeStyle(formattedDateStr) | ||
level := infoStyle(strings.ToUpper(log.Level)) | ||
|
||
if log.Level == "error" { | ||
level = errorStyle(strings.ToUpper(log.Level)) | ||
} | ||
|
||
message := textStyle(log.Message) | ||
|
||
formatterMsg = fmt.Sprintf("%s %s: %s", timeLog, level, message) | ||
} | ||
|
||
renderedMessage := formatterMsg | ||
|
||
ProvisionLogs.Send(logMessage{message: renderedMessage}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright (C) 2021-2023, Kubefirst | ||
This program is licensed under MIT. | ||
See the LICENSE file for more details. | ||
*/ | ||
package provisionLogs | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
const ( | ||
padding = 2 | ||
maxWidth = 80 | ||
) | ||
|
||
const debounceDuration = time.Second * 10 | ||
|
||
var ( | ||
currentPkgNameStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("211")) | ||
doneStyle = lipgloss.NewStyle().Margin(1, 2) | ||
checkMark = lipgloss.NewStyle().Foreground(lipgloss.Color("42")).SetString("✓") | ||
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render | ||
StatusStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF")).Bold(true).Render | ||
spinnerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("69")) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Copyright (C) 2021-2023, Kubefirst | ||
This program is licensed under MIT. | ||
See the LICENSE file for more details. | ||
Emojis definition https://github.com/yuin/goldmark-emoji/blob/master/definition/github.go | ||
Color definition https://www.ditig.com/256-colors-cheat-sheet | ||
*/ | ||
package provisionLogs | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/charmbracelet/glamour" | ||
"github.com/kubefirst/kubefirst/internal/progress" | ||
) | ||
|
||
func renderMessage(message string) string { | ||
r, _ := glamour.NewTermRenderer( | ||
glamour.WithStyles(progress.StyleConfig), | ||
glamour.WithEmoji(), | ||
) | ||
|
||
out, err := r.Render(message) | ||
if err != nil { | ||
log.Println(err.Error()) | ||
return err.Error() | ||
} | ||
return out | ||
} |
Oops, something went wrong.