Skip to content

Commit

Permalink
feat(cmd/token_expiration_alert): Create a method how to easily detec…
Browse files Browse the repository at this point in the history
…t expired tokens in Gitlab CI
  • Loading branch information
ondrejsika committed May 24, 2024
1 parent 0b56a2f commit 5aef79e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ import (
_ "github.com/sikalabs/slu/cmd/tls/parse_file"
_ "github.com/sikalabs/slu/cmd/tls/parse_k8s_secret"
_ "github.com/sikalabs/slu/cmd/tls/pem_to_pfx"
_ "github.com/sikalabs/slu/cmd/token_expiration_alert"
_ "github.com/sikalabs/slu/cmd/up"
_ "github.com/sikalabs/slu/cmd/upload"
_ "github.com/sikalabs/slu/cmd/version"
Expand Down
88 changes: 88 additions & 0 deletions cmd/token_expiration_alert/token_expiration_alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package token_expiration_alert

import (
"fmt"
"log"
"os"
"strings"
"time"

"github.com/sikalabs/slu/cmd/root"
"github.com/spf13/cobra"
)

var FlagExpirationDate string
var FlagMessage string

func init() {
root.RootCmd.AddCommand(Cmd)
Cmd.Flags().StringVarP(&FlagExpirationDate, "expiration-date", "e", "", "Token expiration date (eg. 2024-01-01)")
Cmd.MarkFlagRequired("expiration-date")
Cmd.Flags().StringVarP(&FlagMessage, "message", "m", "", "Message to send")
Cmd.MarkFlagRequired("message")
}

var Cmd = &cobra.Command{
Use: "token-expiration-alert",
Short: "Alert about token expiration",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
expirationAlert(FlagExpirationDate, FlagMessage)
},
}

func expirationAlert(expitationDateString, message string) {
expirationDate, err := time.Parse("2006-01-02", expitationDateString)
if err != nil {
log.Fatalln(err)
return
}

currentDate := time.Now()

// Check if the token has expired
if currentDate.After(expirationDate) || currentDate.Equal(expirationDate) {
printInBox(message)
os.Exit(1)
}
}

func printInBox(text string) {
const maxLineLength = 60
lines := splitText(text, maxLineLength)

// Determine the length of the longest line
maxLength := len(lines[0])
for _, line := range lines {
if len(line) > maxLength {
maxLength = len(line)
}
}

// Create the top and bottom border
borderLength := maxLength + 8 // Additional 8 to account for padding and border
border := strings.Repeat("#", borderLength)
emptyLine := "#" + strings.Repeat(" ", borderLength-2) + "#"

// Print the box with the text
fmt.Println(border)
fmt.Println(emptyLine)
fmt.Println(emptyLine)
for _, line := range lines {
fmt.Printf("# %-*s #\n", maxLength, line)
}
fmt.Println(emptyLine)
fmt.Println(emptyLine)
fmt.Println(border)
}

// Helper function to split text into lines of a specific length
func splitText(text string, length int) []string {
var lines []string
for len(text) > length {
lines = append(lines, text[:length])
text = text[length:]
}
lines = append(lines, text)
return lines
}

0 comments on commit 5aef79e

Please sign in to comment.