textutils: move colors to textutils

This commit is contained in:
2026-02-14 18:33:41 -08:00
parent ba89f0b818
commit d2b3a8173f
2 changed files with 25 additions and 22 deletions

View File

@@ -8,6 +8,8 @@ import (
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
)
type DBConfig struct {
@@ -22,20 +24,6 @@ type DBConfig struct {
version_number uint
}
// Colors for terminal output
const (
ColorReset = "\033[0m"
ColorBlack = "\033[30m"
ColorRed = "\033[31m"
ColorGreen = "\033[32m"
ColorYellow = "\033[33m"
ColorBlue = "\033[34m"
ColorPurple = "\033[35m"
ColorCyan = "\033[36m"
ColorGray = "\033[37m"
ColorWhite = "\033[97m"
)
func Init(schema *string, migrationsList *[]string) DBConfig {
return DBConfig{
sql_schema: schema,
@@ -96,11 +84,11 @@ func (c DBConfig) CheckAndUpdateVersion(db *sqlx.DB) error {
}
if c.version_number > version {
fmt.Print(ColorYellow)
fmt.Print(textutils.ColorYellow)
fmt.Printf("================================================\n")
fmt.Printf("Database version is out of date. Upgrading database from version %d to version %d!\n", version,
c.version_number)
fmt.Print(ColorReset)
fmt.Print(textutils.ColorReset)
c.UpgradeFromXToY(db, version, c.version_number)
}
@@ -110,9 +98,9 @@ func (c DBConfig) CheckAndUpdateVersion(db *sqlx.DB) error {
// UpgradeFromXToY runs all the migrations from version X to version Y, and update the `database_version` table's `version_number`
func (c DBConfig) UpgradeFromXToY(db *sqlx.DB, x uint, y uint) {
for i := x; i < y; i++ {
fmt.Print(ColorCyan)
fmt.Print(textutils.ColorCyan)
fmt.Println((*c.migrations)[i])
fmt.Print(ColorReset)
fmt.Print(textutils.ColorReset)
// Execute the migration in a transaction
tx, err := db.Beginx()
@@ -125,14 +113,14 @@ func (c DBConfig) UpgradeFromXToY(db *sqlx.DB, x uint, y uint) {
panic(err)
}
fmt.Print(ColorYellow)
fmt.Print(textutils.ColorYellow)
fmt.Printf("Now at database schema version %d.\n", i+1)
fmt.Print(ColorReset)
fmt.Print(textutils.ColorReset)
}
fmt.Print(ColorGreen)
fmt.Print(textutils.ColorGreen)
fmt.Printf("================================================\n")
fmt.Printf("Database version has been upgraded to version %d.\n", y)
fmt.Print(ColorReset)
fmt.Print(textutils.ColorReset)
}
type VersionMismatchError struct {

View File

@@ -0,0 +1,15 @@
package textutils
// Colors for terminal output
const (
ColorReset = "\033[0m"
ColorBlack = "\033[30m"
ColorRed = "\033[31m"
ColorGreen = "\033[32m"
ColorYellow = "\033[33m"
ColorBlue = "\033[34m"
ColorPurple = "\033[35m"
ColorCyan = "\033[36m"
ColorGray = "\033[37m"
ColorWhite = "\033[97m"
)