2023-09-27 20:25:38 +08:00
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package cmd
import (
2025-06-01 22:16:37 +02:00
"context"
2024-04-22 03:44:03 +08:00
"errors"
2023-09-27 20:25:38 +08:00
"fmt"
"os"
"text/tabwriter"
2025-03-27 19:40:14 +00:00
auth_model "forgejo.org/models/auth"
"forgejo.org/models/db"
auth_service "forgejo.org/services/auth"
2023-09-27 20:25:38 +08:00
2025-06-01 22:16:37 +02:00
"github.com/urfave/cli/v3"
2023-09-27 20:25:38 +08:00
)
2025-06-01 22:16:37 +02:00
func microcmdAuthDelete ( ) * cli . Command {
return & cli . Command {
2023-09-27 20:25:38 +08:00
Name : "delete" ,
Usage : "Delete specific auth source" ,
2025-06-01 22:16:37 +02:00
Flags : [ ] cli . Flag { idFlag ( ) } ,
2023-09-27 20:25:38 +08:00
Action : runDeleteAuth ,
}
2025-06-01 22:16:37 +02:00
}
func microcmdAuthList ( ) * cli . Command {
return & cli . Command {
2023-09-27 20:25:38 +08:00
Name : "list" ,
Usage : "List auth sources" ,
Action : runListAuth ,
Flags : [ ] cli . Flag {
& cli . IntFlag {
Name : "min-width" ,
Usage : "Minimal cell width including any padding for the formatted table" ,
Value : 0 ,
} ,
& cli . IntFlag {
Name : "tab-width" ,
Usage : "width of tab characters in formatted table (equivalent number of spaces)" ,
Value : 8 ,
} ,
& cli . IntFlag {
Name : "padding" ,
Usage : "padding added to a cell before computing its width" ,
Value : 1 ,
} ,
& cli . StringFlag {
Name : "pad-char" ,
Usage : ` ASCII char used for padding if padchar == '\\t', the Writer will assume that the width of a '\\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result) ` ,
Value : "\t" ,
} ,
& cli . BoolFlag {
Name : "vertical-bars" ,
Usage : "Set to true to print vertical bars between columns" ,
} ,
} ,
}
2025-06-01 22:16:37 +02:00
}
2023-09-27 20:25:38 +08:00
2025-06-01 22:16:37 +02:00
func runListAuth ( ctx context . Context , c * cli . Command ) error {
ctx , cancel := installSignals ( ctx )
2023-09-27 20:25:38 +08:00
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
return err
}
2023-11-24 11:49:41 +08:00
authSources , err := db . Find [ auth_model . Source ] ( ctx , auth_model . FindSourcesOptions { } )
2023-09-27 20:25:38 +08:00
if err != nil {
return err
}
flags := tabwriter . AlignRight
if c . Bool ( "vertical-bars" ) {
flags |= tabwriter . Debug
}
padChar := byte ( '\t' )
if len ( c . String ( "pad-char" ) ) > 0 {
padChar = c . String ( "pad-char" ) [ 0 ]
}
// loop through each source and print
w := tabwriter . NewWriter ( os . Stdout , c . Int ( "min-width" ) , c . Int ( "tab-width" ) , c . Int ( "padding" ) , padChar , flags )
2025-05-29 17:34:29 +02:00
fmt . Fprint ( w , "ID\tName\tType\tEnabled\n" )
2023-09-27 20:25:38 +08:00
for _ , source := range authSources {
fmt . Fprintf ( w , "%d\t%s\t%s\t%t\n" , source . ID , source . Name , source . Type . String ( ) , source . IsActive )
}
w . Flush ( )
return nil
}
2025-06-01 22:16:37 +02:00
func runDeleteAuth ( ctx context . Context , c * cli . Command ) error {
2023-09-27 20:25:38 +08:00
if ! c . IsSet ( "id" ) {
2024-04-22 03:44:03 +08:00
return errors . New ( "--id flag is missing" )
2023-09-27 20:25:38 +08:00
}
2025-06-01 22:16:37 +02:00
ctx , cancel := installSignals ( ctx )
2023-09-27 20:25:38 +08:00
defer cancel ( )
if err := initDB ( ctx ) ; err != nil {
return err
}
2023-10-11 06:24:07 +02:00
source , err := auth_model . GetSourceByID ( ctx , c . Int64 ( "id" ) )
2023-09-27 20:25:38 +08:00
if err != nil {
return err
}
2023-10-14 10:37:24 +02:00
return auth_service . DeleteSource ( ctx , source )
2023-09-27 20:25:38 +08:00
}