refractor: add initial go internall pkg module.
This commit is contained in:
parent
ef8029e086
commit
049b939349
4 changed files with 289 additions and 0 deletions
90
pkg/logging/logging.go
Normal file
90
pkg/logging/logging.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
// apparmor.d - Full set of apparmor profiles
|
||||
// Copyright (C) 2023 Alexandre Pujol <alexandre@pujol.io>
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Colors
|
||||
const (
|
||||
reset = "\033[0m"
|
||||
bold = "\033[1m"
|
||||
boldRed = "\033[1;31m"
|
||||
boldGreen = "\033[1;32m"
|
||||
boldYellow = "\033[1;33m"
|
||||
)
|
||||
|
||||
// Logging messages prefix
|
||||
const (
|
||||
bulletText = bold + " ⋅ " + reset
|
||||
errorText = boldRed + " ✗ Error: " + reset
|
||||
successText = boldGreen + " ✓ " + reset
|
||||
warningText = boldYellow + " ‼ " + reset
|
||||
)
|
||||
|
||||
// Print prints a formatted message. Arguments are handled in the manner of fmt.Print.
|
||||
func Print(msg string, a ...interface{}) int {
|
||||
n, _ := fmt.Fprintf(os.Stdout, msg, a...)
|
||||
return n
|
||||
}
|
||||
|
||||
// Println prints a formatted message. Arguments are handled in the manner of fmt.Println.
|
||||
func Println(msg string) int {
|
||||
n, _ := fmt.Fprintf(os.Stdout, msg+"\n")
|
||||
return n
|
||||
}
|
||||
|
||||
// Bulletf returns a formatted bullet point string
|
||||
func Bulletf(msg string, a ...interface{}) string {
|
||||
return fmt.Sprintf("%s%s\n", bulletText, fmt.Sprintf(msg, a...))
|
||||
}
|
||||
|
||||
// Bullet prints a formatted bullet point string
|
||||
func Bullet(msg string, a ...interface{}) int {
|
||||
return Print(Bulletf(msg, a...))
|
||||
}
|
||||
|
||||
// Stepf returns a formatted step string
|
||||
func Stepf(msg string, a ...interface{}) string {
|
||||
return fmt.Sprintf("%s%s\033[0m\n", boldGreen, fmt.Sprintf(msg, a...))
|
||||
}
|
||||
|
||||
// Step prints a step title
|
||||
func Step(msg string, a ...interface{}) int {
|
||||
return Print(Stepf(msg, a...))
|
||||
}
|
||||
|
||||
// Successf returns a formatted success string
|
||||
func Successf(msg string, a ...interface{}) string {
|
||||
return fmt.Sprintf("%s%s\n", successText, fmt.Sprintf(msg, a...))
|
||||
}
|
||||
|
||||
// Success prints a formatted success message to stdout
|
||||
func Success(msg string, a ...interface{}) int {
|
||||
return Print(Successf(msg, a...))
|
||||
}
|
||||
|
||||
// Warningf returns a formatted warning string
|
||||
func Warningf(msg string, a ...interface{}) string {
|
||||
return fmt.Sprintf("%s%s\n", warningText, fmt.Sprintf(msg, a...))
|
||||
}
|
||||
|
||||
// Warning prints a formatted warning message to stdout
|
||||
func Warning(msg string, a ...interface{}) int {
|
||||
return Print(Warningf(msg, a...))
|
||||
}
|
||||
|
||||
// Fatalf returns a formatted error message
|
||||
func Fatalf(msg string, a ...interface{}) string {
|
||||
return fmt.Sprintf("%s%s\n", errorText, fmt.Sprintf(msg, a...))
|
||||
}
|
||||
|
||||
// Fatal is equivalent to Print() followed by a call to os.Exit(1).
|
||||
func Fatal(msg string, a ...interface{}) {
|
||||
fmt.Fprint(os.Stderr, Fatalf(msg, a...))
|
||||
os.Exit(1)
|
||||
}
|
||||
102
pkg/logging/logging_test.go
Normal file
102
pkg/logging/logging_test.go
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
// apparmor.d - Full set of apparmor profiles
|
||||
// Copyright (C) 2023 Alexandre Pujol <alexandre@pujol.io>
|
||||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
|
||||
package logging
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPrint(t *testing.T) {
|
||||
msg := "Print message"
|
||||
wantN := 13
|
||||
|
||||
gotN := Print(msg)
|
||||
if gotN != wantN {
|
||||
t.Errorf("Print() = %v, want %v", gotN, wantN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintln(t *testing.T) {
|
||||
msg := "Print message"
|
||||
wantN := 14
|
||||
gotN := Println(msg)
|
||||
if gotN != wantN {
|
||||
t.Errorf("Println() = %v, want %v", gotN, wantN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBulletf(t *testing.T) {
|
||||
msg := "Bullet message"
|
||||
want := "\033[1m ⋅ \033[0mBullet message\n"
|
||||
if got := Bulletf(msg); got != want {
|
||||
t.Errorf("Bulletf() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBullet(t *testing.T) {
|
||||
msg := "Bullet message"
|
||||
wantN := 28
|
||||
gotN := Bullet(msg)
|
||||
if gotN != wantN {
|
||||
t.Errorf("Bullet() = %v, want %v", gotN, wantN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStepf(t *testing.T) {
|
||||
msg := "Step message"
|
||||
want := "\033[1;32mStep message\033[0m\n"
|
||||
if got := Stepf(msg); got != want {
|
||||
t.Errorf("Stepf() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStep(t *testing.T) {
|
||||
msg := "Step message"
|
||||
wantN := 24
|
||||
gotN := Step(msg)
|
||||
if gotN != wantN {
|
||||
t.Errorf("Step() = %v, want %v", gotN, wantN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuccessf(t *testing.T) {
|
||||
msg := "Success message"
|
||||
want := "\033[1;32m ✓ \033[0mSuccess message\n"
|
||||
if got := Successf(msg); got != want {
|
||||
t.Errorf("Successf() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuccess(t *testing.T) {
|
||||
msg := "Success message"
|
||||
wantN := 32
|
||||
gotN := Success(msg)
|
||||
if gotN != wantN {
|
||||
t.Errorf("Success() = %v, want %v", gotN, wantN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWarningf(t *testing.T) {
|
||||
msg := "Warning message"
|
||||
want := "\033[1;33m ‼ \033[0mWarning message\n"
|
||||
if got := Warningf(msg); got != want {
|
||||
t.Errorf("Warningf() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWarning(t *testing.T) {
|
||||
msg := "Warning message"
|
||||
wantN := 32
|
||||
gotN := Warning(msg)
|
||||
if gotN != wantN {
|
||||
t.Errorf("Warning() = %v, want %v", gotN, wantN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFatalf(t *testing.T) {
|
||||
msg := "Error message"
|
||||
want := "\033[1;31m ✗ Error: \033[0mError message\n"
|
||||
if got := Fatalf(msg); got != want {
|
||||
t.Errorf("Fatalf() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue