mirror of
				https://github.com/kovetskiy/mark.git
				synced 2025-11-04 06:17:36 +08:00 
			
		
		
		
	Log levels support
This commit is contained in:
		
							parent
							
								
									2ba35118bf
								
							
						
					
					
						commit
						f25d8876fc
					
				@ -794,8 +794,7 @@ GLOBAL OPTIONS:
 | 
				
			|||||||
   --minor-edit                                  don't send notifications while updating Confluence page. (default: false) [$MARK_MINOR_EDIT]
 | 
					   --minor-edit                                  don't send notifications while updating Confluence page. (default: false) [$MARK_MINOR_EDIT]
 | 
				
			||||||
   --version-message value                       add a message to the page version, to explain the edit (default: "") [$MARK_VERSION_MESSAGE]
 | 
					   --version-message value                       add a message to the page version, to explain the edit (default: "") [$MARK_VERSION_MESSAGE]
 | 
				
			||||||
   --color value                                 display logs in color. Possible values: auto, never. (default: "auto") [$MARK_COLOR]
 | 
					   --color value                                 display logs in color. Possible values: auto, never. (default: "auto") [$MARK_COLOR]
 | 
				
			||||||
   --debug                                       enable debug logs. (default: false) [$MARK_DEBUG]
 | 
					   --log-level value                             set the log level. Possible values: TRACE, DEBUG, INFO, WARNING, ERROR, FATAL. (default: "info") [$MARK_LOG_LEVEL]
 | 
				
			||||||
   --trace                                       enable trace logs. (default: false) [$MARK_TRACE]
 | 
					 | 
				
			||||||
   --username value, -u value                    use specified username for updating Confluence page. [$MARK_USERNAME]
 | 
					   --username value, -u value                    use specified username for updating Confluence page. [$MARK_USERNAME]
 | 
				
			||||||
   --password value, -p value                    use specified token for updating Confluence page. Specify - as password to read password from stdin, or your Personal access token. Username is not mandatory if personal access token is provided. For more info please see: https://developer.atlassian.com/server/confluence/confluence-server-rest-api/#authentication. [$MARK_PASSWORD]
 | 
					   --password value, -p value                    use specified token for updating Confluence page. Specify - as password to read password from stdin, or your Personal access token. Username is not mandatory if personal access token is provided. For more info please see: https://developer.atlassian.com/server/confluence/confluence-server-rest-api/#authentication. [$MARK_PASSWORD]
 | 
				
			||||||
   --target-url value, -l value                  edit specified Confluence page. If -l is not specified, file should contain metadata (see above). [$MARK_TARGET_URL]
 | 
					   --target-url value, -l value                  edit specified Confluence page. If -l is not specified, file should contain metadata (see above). [$MARK_TARGET_URL]
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										49
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								main.go
									
									
									
									
									
								
							@ -105,17 +105,11 @@ var flags = []cli.Flag{
 | 
				
			|||||||
		Usage:   "display logs in color. Possible values: auto, never.",
 | 
							Usage:   "display logs in color. Possible values: auto, never.",
 | 
				
			||||||
		EnvVars: []string{"MARK_COLOR"},
 | 
							EnvVars: []string{"MARK_COLOR"},
 | 
				
			||||||
	}),
 | 
						}),
 | 
				
			||||||
	altsrc.NewBoolFlag(&cli.BoolFlag{
 | 
						altsrc.NewStringFlag(&cli.StringFlag{
 | 
				
			||||||
		Name:    "debug",
 | 
							Name:    "log-level",
 | 
				
			||||||
		Value:   false,
 | 
							Value:   "info",
 | 
				
			||||||
		Usage:   "enable debug logs.",
 | 
							Usage:   "set the log level. Possible values: TRACE, DEBUG, INFO, WARNING, ERROR, FATAL.",
 | 
				
			||||||
		EnvVars: []string{"MARK_DEBUG"},
 | 
							EnvVars: []string{"MARK_LOG_LEVEL"},
 | 
				
			||||||
	}),
 | 
					 | 
				
			||||||
	altsrc.NewBoolFlag(&cli.BoolFlag{
 | 
					 | 
				
			||||||
		Name:    "trace",
 | 
					 | 
				
			||||||
		Value:   false,
 | 
					 | 
				
			||||||
		Usage:   "enable trace logs.",
 | 
					 | 
				
			||||||
		EnvVars: []string{"MARK_TRACE"},
 | 
					 | 
				
			||||||
	}),
 | 
						}),
 | 
				
			||||||
	altsrc.NewStringFlag(&cli.StringFlag{
 | 
						altsrc.NewStringFlag(&cli.StringFlag{
 | 
				
			||||||
		Name:    "username",
 | 
							Name:    "username",
 | 
				
			||||||
@ -230,13 +224,8 @@ func main() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func RunMark(cCtx *cli.Context) error {
 | 
					func RunMark(cCtx *cli.Context) error {
 | 
				
			||||||
 | 
						if err := setLogLevel(cCtx); err != nil {
 | 
				
			||||||
	if cCtx.Bool("debug") {
 | 
							return err
 | 
				
			||||||
		log.SetLevel(lorg.LevelDebug)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if cCtx.Bool("trace") {
 | 
					 | 
				
			||||||
		log.SetLevel(lorg.LevelTrace)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if cCtx.String("color") == "never" {
 | 
						if cCtx.String("color") == "never" {
 | 
				
			||||||
@ -551,7 +540,6 @@ func processFile(
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func updateLabels(api *confluence.API, target *confluence.PageInfo, meta *metadata.Meta) {
 | 
					func updateLabels(api *confluence.API, target *confluence.PageInfo, meta *metadata.Meta) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	labelInfo, err := api.GetPageLabels(target, "global")
 | 
						labelInfo, err := api.GetPageLabels(target, "global")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatal(err)
 | 
							log.Fatal(err)
 | 
				
			||||||
@ -619,3 +607,26 @@ func configFilePath() string {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return filepath.Join(fp, "mark")
 | 
						return filepath.Join(fp, "mark")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func setLogLevel(cCtx *cli.Context) error {
 | 
				
			||||||
 | 
						logLevel := cCtx.String("log-level")
 | 
				
			||||||
 | 
						switch logLevel {
 | 
				
			||||||
 | 
						case "TRACE":
 | 
				
			||||||
 | 
							log.SetLevel(lorg.LevelTrace)
 | 
				
			||||||
 | 
						case "DEBUG":
 | 
				
			||||||
 | 
							log.SetLevel(lorg.LevelDebug)
 | 
				
			||||||
 | 
						case "INFO":
 | 
				
			||||||
 | 
							log.SetLevel(lorg.LevelInfo)
 | 
				
			||||||
 | 
						case "WARNING":
 | 
				
			||||||
 | 
							log.SetLevel(lorg.LevelWarning)
 | 
				
			||||||
 | 
						case "ERROR":
 | 
				
			||||||
 | 
							log.SetLevel(lorg.LevelError)
 | 
				
			||||||
 | 
						case "FATAL":
 | 
				
			||||||
 | 
							log.SetLevel(lorg.LevelFatal)
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return fmt.Errorf("unknown log level: %s", logLevel)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						log.GetLevel()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										45
									
								
								main_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								main_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,45 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"flag"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/reconquest/pkg/log"
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
 | 
						"github.com/urfave/cli/v2"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func Test_setLogLevel(t *testing.T) {
 | 
				
			||||||
 | 
						type args struct {
 | 
				
			||||||
 | 
							lvl string
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						tests := map[string]struct {
 | 
				
			||||||
 | 
							args        args
 | 
				
			||||||
 | 
							want        log.Level
 | 
				
			||||||
 | 
							expectedErr string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							"invalid": {args: args{lvl: "INVALID"}, want: log.LevelInfo, expectedErr: "unknown log level: INVALID"},
 | 
				
			||||||
 | 
							"empty":   {args: args{lvl: ""}, want: log.LevelInfo, expectedErr: "unknown log level: "},
 | 
				
			||||||
 | 
							"info":    {args: args{lvl: log.LevelInfo.String()}, want: log.LevelInfo},
 | 
				
			||||||
 | 
							"debug":   {args: args{lvl: log.LevelDebug.String()}, want: log.LevelDebug},
 | 
				
			||||||
 | 
							"trace":   {args: args{lvl: log.LevelTrace.String()}, want: log.LevelTrace},
 | 
				
			||||||
 | 
							"warning": {args: args{lvl: log.LevelWarning.String()}, want: log.LevelWarning},
 | 
				
			||||||
 | 
							"error":   {args: args{lvl: log.LevelError.String()}, want: log.LevelError},
 | 
				
			||||||
 | 
							"fatal":   {args: args{lvl: log.LevelFatal.String()}, want: log.LevelFatal},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for name, tt := range tests {
 | 
				
			||||||
 | 
							t.Run(name, func(t *testing.T) {
 | 
				
			||||||
 | 
								set := flag.NewFlagSet("test", flag.ContinueOnError)
 | 
				
			||||||
 | 
								set.String("log-level", tt.args.lvl, "")
 | 
				
			||||||
 | 
								cliCtx := cli.NewContext(nil, set, nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								err := setLogLevel(cliCtx)
 | 
				
			||||||
 | 
								if tt.expectedErr != "" {
 | 
				
			||||||
 | 
									assert.EqualError(t, err, tt.expectedErr)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									assert.NoError(t, err)
 | 
				
			||||||
 | 
									assert.Equal(t, tt.want, log.GetLevel())
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user