Skip to content

Commit 2c7f907

Browse files
wip
1 parent dec85a5 commit 2c7f907

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

cmd/cmdutils/flags.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cmdutils
2+
3+
import (
4+
"codacy/cli-v2/domain"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// AddCloudFlags adds the common cloud-related flags to a cobra command.
10+
// The flags will be bound to the provided flags struct.
11+
func AddCloudFlags(cmd *cobra.Command, flags *domain.InitFlags) {
12+
cmd.Flags().StringVar(&flags.ApiToken, "api-token", "", "Optional Codacy API token. If defined, configurations will be fetched from Codacy")
13+
cmd.Flags().StringVar(&flags.Provider, "provider", "", "Provider (e.g., gh, bb, gl) to fetch configurations from Codacy. Required when api-token is provided")
14+
cmd.Flags().StringVar(&flags.Organization, "organization", "", "Remote organization name to fetch configurations from Codacy. Required when api-token is provided")
15+
cmd.Flags().StringVar(&flags.Repository, "repository", "", "Remote repository name to fetch configurations from Codacy. Required when api-token is provided")
16+
}

cmd/config.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sort"
1010
"strings"
1111

12+
"codacy/cli-v2/cmd/cmdutils"
1213
"codacy/cli-v2/cmd/configsetup"
1314
codacyclient "codacy/cli-v2/codacy-client"
1415
"codacy/cli-v2/config"
@@ -258,15 +259,38 @@ func detectLanguagesInPath(rootPath string, toolLangMap map[string]domain.ToolLa
258259
return nil, fmt.Errorf("failed to detect file extensions in path %s: %w", rootPath, err)
259260
}
260261

261-
// Map extensions to languages
262+
// Map only found extensions to languages
262263
for ext := range extCount {
263264
if langs, ok := extToLang[ext]; ok {
265+
// Log which extensions map to which languages for debugging
266+
logger.Debug("Found files with extension", logrus.Fields{
267+
"extension": ext,
268+
"count": extCount[ext],
269+
"languages": langs,
270+
})
264271
for _, lang := range langs {
265272
detectedLangs[lang] = struct{}{}
266273
}
267274
}
268275
}
269276

277+
// Log the final set of detected languages with their corresponding extensions
278+
if len(detectedLangs) > 0 {
279+
langToExts := make(map[string][]string)
280+
for ext, count := range extCount {
281+
if langs, ok := extToLang[ext]; ok {
282+
for _, lang := range langs {
283+
langToExts[lang] = append(langToExts[lang], fmt.Sprintf("%s (%d files)", ext, count))
284+
}
285+
}
286+
}
287+
288+
logger.Debug("Detected languages in path", logrus.Fields{
289+
"languages_with_files": langToExts,
290+
"path": discoverPath,
291+
})
292+
}
293+
270294
return detectedLangs, nil
271295
}
272296

@@ -623,22 +647,13 @@ func getRecognizableExtensions(extCount map[string]int, toolLangMap map[string]d
623647
}
624648

625649
func init() {
626-
// Define flags for the config reset command. These are the same flags used by the init command.
627-
configResetCmd.Flags().StringVar(&configResetInitFlags.ApiToken, "api-token", "", "Optional Codacy API token. If defined, configurations will be fetched from Codacy.")
628-
configResetCmd.Flags().StringVar(&configResetInitFlags.Provider, "provider", "", "Provider (e.g., gh, bb, gl) to fetch configurations from Codacy. Required when api-token is provided.")
629-
configResetCmd.Flags().StringVar(&configResetInitFlags.Organization, "organization", "", "Remote organization name to fetch configurations from Codacy. Required when api-token is provided.")
630-
configResetCmd.Flags().StringVar(&configResetInitFlags.Repository, "repository", "", "Remote repository name to fetch configurations from Codacy. Required when api-token is provided.")
631-
632-
// Add the reset subcommand to the config command
633-
configCmd.AddCommand(configResetCmd)
634-
// Add the discover subcommand to the config command
635-
configCmd.AddCommand(configDiscoverCmd)
636-
// Add the config command to the root command
637-
rootCmd.AddCommand(configCmd)
650+
// Add cloud-related flags to both commands
651+
cmdutils.AddCloudFlags(configResetCmd, &configResetInitFlags)
652+
cmdutils.AddCloudFlags(configDiscoverCmd, &configResetInitFlags)
638653

639-
// Add cloud-related flags to the discover command
640-
configDiscoverCmd.Flags().StringVar(&configResetInitFlags.ApiToken, "api-token", "", "Optional Codacy API token. If defined, configurations will be fetched from Codacy.")
641-
configDiscoverCmd.Flags().StringVar(&configResetInitFlags.Provider, "provider", "", "Provider (e.g., gh, bb, gl) to fetch configurations from Codacy. Required when api-token is provided.")
642-
configDiscoverCmd.Flags().StringVar(&configResetInitFlags.Organization, "organization", "", "Remote organization name to fetch configurations from Codacy. Required when api-token is provided.")
643-
configDiscoverCmd.Flags().StringVar(&configResetInitFlags.Repository, "repository", "", "Remote repository name to fetch configurations from Codacy. Required when api-token is provided.")
654+
// Add subcommands to config command
655+
configCmd.AddCommand(configResetCmd, configDiscoverCmd)
656+
657+
// Add config command to root
658+
rootCmd.AddCommand(configCmd)
644659
}

cmd/init.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"codacy/cli-v2/cmd/cmdutils"
45
"codacy/cli-v2/cmd/configsetup"
56
"codacy/cli-v2/config"
67
"codacy/cli-v2/domain"
@@ -14,10 +15,8 @@ import (
1415
var initFlags domain.InitFlags
1516

1617
func init() {
17-
initCmd.Flags().StringVar(&initFlags.ApiToken, "api-token", "", "optional codacy api token, if defined configurations will be fetched from codacy")
18-
initCmd.Flags().StringVar(&initFlags.Provider, "provider", "", "provider (gh/bb/gl) to fetch configurations from codacy, required when api-token is provided")
19-
initCmd.Flags().StringVar(&initFlags.Organization, "organization", "", "remote organization name to fetch configurations from codacy, required when api-token is provided")
20-
initCmd.Flags().StringVar(&initFlags.Repository, "repository", "", "remote repository name to fetch configurations from codacy, required when api-token is provided")
18+
// Add cloud-related flags
19+
cmdutils.AddCloudFlags(initCmd, &initFlags)
2120
rootCmd.AddCommand(initCmd)
2221
}
2322

0 commit comments

Comments
 (0)