Skip to content

Commit cf31822

Browse files
wip
1 parent a3773b4 commit cf31822

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
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 & 10 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"
@@ -257,15 +258,38 @@ func detectLanguagesInPath(rootPath string, toolLangMap map[string]domain.ToolLa
257258
return nil, fmt.Errorf("failed to detect file extensions in path %s: %w", rootPath, err)
258259
}
259260

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

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

@@ -622,14 +646,13 @@ func getRecognizableExtensions(extCount map[string]int, toolLangMap map[string]d
622646
}
623647

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

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)