MCPcopy Create free account
hub / github.com/github/gh-aw / addAgentFileWithTracking

Function addAgentFileWithTracking

pkg/cli/add_command.go:801–846  ·  view source on GitHub ↗

addAgentFileWithTracking installs a single agent file from a package to the agentic engine agents directory.

(resolved *ResolvedWorkflow, tracker *FileTracker, opts AddOptions, gitRoot string)

Source from the content-addressed store, hash-verified

799// addAgentFileWithTracking installs a single agent file from a package to the agentic engine
800// agents directory.
801func addAgentFileWithTracking(resolved *ResolvedWorkflow, tracker *FileTracker, opts AddOptions, gitRoot string) error {
802 engineAgentsDir := workflow.GetEngineSubAgentDir(opts.EngineOverride)
803 agentsDir := filepath.Join(gitRoot, engineAgentsDir)
804 if err := os.MkdirAll(agentsDir, constants.DirPermPublic); err != nil {
805 return fmt.Errorf("failed to create agents directory %s: %w", agentsDir, err)
806 }
807
808 fileName := filepath.Base(resolved.Spec.WorkflowPath)
809 destFile := filepath.Join(agentsDir, fileName)
810
811 addLog.Printf("Adding agent file: dest=%s, content_size=%d bytes", destFile, len(resolved.Content))
812
813 if opts.Verbose {
814 fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Adding agent file to %s: %s", engineAgentsDir, fileName)))
815 }
816
817 fileExists := false
818 if fileutil.FileExists(destFile) {
819 fileExists = true
820 if !opts.Force {
821 if opts.Verbose {
822 fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Agent file '%s' already exists. Skipping.", destFile)))
823 }
824 return nil
825 }
826 fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Overwriting existing agent file: "+destFile))
827 }
828
829 if tracker != nil {
830 if fileExists {
831 tracker.TrackModified(destFile)
832 } else {
833 tracker.TrackCreated(destFile)
834 }
835 }
836
837 if err := os.WriteFile(destFile, resolved.Content, constants.FilePermPublic); err != nil {
838 return fmt.Errorf("failed to write agent file '%s': %w", destFile, err)
839 }
840
841 if !opts.Quiet {
842 fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(fmt.Sprintf("Added agent file: %s/%s", engineAgentsDir, fileName)))
843 }
844
845 return nil
846}
847
848// printCompilationError formats and writes a compilation error to stderr.
849// Redirect-only workflow errors are treated as informational messages rather than errors,

Callers 1

addWorkflowWithTrackingFunction · 0.85

Calls 9

GetEngineSubAgentDirFunction · 0.92
FormatInfoMessageFunction · 0.92
FileExistsFunction · 0.92
FormatWarningMessageFunction · 0.92
FormatSuccessMessageFunction · 0.92
TrackModifiedMethod · 0.80
TrackCreatedMethod · 0.65
ErrorfMethod · 0.45
PrintfMethod · 0.45

Tested by

no test coverage detected