func newInitCommand()

in internal/cmd/init.go [34:115]


func newInitCommand() *cobra.Command {
	cliOptions := &initOptions{}
	cmd := &cobra.Command{
		Use:   "init",
		Short: "Configure a project for Qodana",
		Long:  `Configure a project for Qodana: prepare Qodana configuration file by analyzing the project structure and generating a default configuration qodana.yaml file.`,
		Run: func(cmd *cobra.Command, args []string) {
			qdenv.InitializeQodanaGlobalEnv(qdenv.EmptyEnvProvider())

			localQodanaYamlFullPath := qdyaml.GetLocalNotEffectiveQodanaYamlFullPath(
				cliOptions.ProjectDir,
				cliOptions.ConfigName,
			)
			if localQodanaYamlFullPath == "" {
				localQodanaYamlFullPath = filepath.Join(cliOptions.ProjectDir, "qodana.yaml")
			}
			qodanaYaml := qdyaml.LoadQodanaYamlByFullPath(localQodanaYamlFullPath)

			ide := qodanaYaml.Ide
			linter := qodanaYaml.Linter
			if (linter == "" && ide == "") || cliOptions.Force {
				absPath, err := filepath.Abs(cliOptions.ProjectDir)
				if err != nil {
					log.Fatal(err)
				}
				cliOptions.ProjectDir = absPath
				if msg.IsInteractive() && !msg.AskUserConfirm(
					fmt.Sprintf(
						"Do you want to set up Qodana in %s",
						msg.PrimaryBold(cliOptions.ProjectDir),
					),
				) {
					return
				}
				token := qdenv.GetQodanaGlobalEnv(qdenv.QodanaToken)
				analyzer := commoncontext.SelectAnalyzerForPath(cliOptions.ProjectDir, token)

				writeQodanaLinterToYamlFileWithWarning(
					localQodanaYamlFullPath,
					analyzer,
				)

				checkToken(analyzer, cliOptions)
			} else {
				msg.EmptyMessage()
				var analyzer string
				if ide != "" {
					analyzer = ide
				} else if linter != "" {
					analyzer = linter
				}
				msg.SuccessMessage(
					"The product to use was already configured before: %s. Run the command with %s flag to re-init the project",
					msg.PrimaryBold(analyzer),
					msg.PrimaryBold("-f"),
				)
			}
			if msg.IsInteractive() && qodanaYaml.IsDotNet() && (qodanaYaml.DotNet.IsEmpty() || cliOptions.Force) {
				if commoncontext.GetAndSaveDotNetConfig(cliOptions.ProjectDir, localQodanaYamlFullPath) {
					msg.SuccessMessage("The .NET configuration was successfully set")
				}
			}
			msg.PrintFile(localQodanaYamlFullPath)
		},
	}
	flags := cmd.Flags()
	flags.StringVarP(&cliOptions.ProjectDir, "project-dir", "i", ".", "Root directory of the project to configure")
	flags.BoolVarP(
		&cliOptions.Force,
		"force",
		"f",
		false,
		"Force initialization (overwrite existing valid qodana.yaml)",
	)
	flags.StringVar(
		&cliOptions.ConfigName,
		"config",
		"",
		"Set a custom configuration file instead of 'qodana.yaml'. Relative paths in the configuration will be based on the project directory.",
	)
	return cmd
}