-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/token_expiration_alert): Create a method how to easily detec…
…t expired tokens in Gitlab CI
- Loading branch information
1 parent
0b56a2f
commit 5aef79e
Showing
2 changed files
with
89 additions
and
0 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
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,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 | ||
} |