flipped-aurora/gin-vue-admin · error

token不能为空

Error message

token不能为空

What it means

The `gva login` cobra command persists a JWT token into the local gva CLI config. It validates the --token flag and rejects an empty/whitespace-only value because saving an empty token would produce an unusable configuration.

Source

Thrown at server/cmd/gva/login.go:17

package main

import (
	"fmt"
	"strings"

	"github.com/spf13/cobra"
)

func newLoginCmd(cfg *CliConfig, configPath string) *cobra.Command {
	var token string
	cmd := &cobra.Command{
		Use:   "login",
		Short: "保存JWT token到本地配置",
		RunE: func(cmd *cobra.Command, args []string) error {
			if strings.TrimSpace(token) == "" {
				return fmt.Errorf("token不能为空")
			}
			cfg.Token = strings.TrimSpace(token)
			return saveConfig(configPath, *cfg)
		},
	}
	cmd.Flags().StringVar(&token, "token", "", "JWT token")
	return cmd
}

func newSetBaseURLCmd(cfg *CliConfig, configPath string) *cobra.Command {
	return &cobra.Command{
		Use:   "set-base-url <url>",
		Short: "更改后台 API 地址并保存到本地配置",
		Args:  cobra.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			cfg.BaseURL = strings.TrimRight(strings.TrimSpace(args[0]), "/")
			return saveConfig(configPath, *cfg)
		},

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Pass a valid token: gva login --token <your-jwt-token>
  2. If using a shell variable, confirm it is non-empty before invoking (echo $TOKEN)
  3. Re-copy the token from the login response; ensure no trimming stripped it

Example fix

// before
gva login --token "$TOKEN"   # TOKEN empty
// after
[ -n "$TOKEN" ] && gva login --token "$TOKEN" || echo 'TOKEN is empty'
Defensive patterns

Strategy: validation

Validate before calling

if strings.TrimSpace(os.Getenv("GVA_TOKEN")) == "" {
    return fmt.Errorf("GVA_TOKEN is empty; run gva login --token <jwt>")
}

Try / catch

if err := loginCmd.Execute(); err != nil {
    if strings.Contains(err.Error(), "token不能为空") {
        fmt.Println("usage: gva login --token <jwt>")
    }
}

Prevention

When it happens

Trigger: Running `gva login` without the --token flag, or with `--token ""` or whitespace-only value.

Common situations: Forgetting the flag syntax; shell variable holding the token is unset/empty; copy-paste failed and produced empty string.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/152298b50b739c23. Report an issue: GitHub.