saving changes

This commit is contained in:
2021-10-09 17:34:15 +04:00
parent a06e1be2bc
commit 56be275759
4 changed files with 78 additions and 2 deletions

View File

@@ -278,5 +278,32 @@ func (grp *Groups)RemoveContainerGroups(Container *TrackContainer) error {
// Set group information to list groups
grp.GroupList[i] = group
}
return nil
}
func (TC *TrackContainer)ModifyContainerGroups() error {
group, err := ReadGroup()
if err != nil {
return err
}
// Iterate though all groups and modify the container
// information in groups where the modified container
// ID matches
for i, _ := range group.GroupList {
// Checking in each group if the modified container ID exists
for j, _:= range group.GroupList[i].TrackContainerList {
// If there is match then change them
if group.GroupList[i].TrackContainerList[j].Id == TC.Id {
group.GroupList[i].TrackContainerList[j] = TC
}
}
}
// Write modified result to the Groups track container JSON file
err = group.WriteGroup()
if err != nil {
return err
}
return nil
}

View File

@@ -146,6 +146,8 @@ func ReadTrackContainers(filename string) (*TrackContainers, error) {
return c, nil
}
//func ModifyTrackContainers()
// GetContainerInformation gets information about container based on
// container ID provided
func GetContainerInformation(ID string) (*TrackContainer, error) {
@@ -164,6 +166,48 @@ func GetContainerInformation(ID string) (*TrackContainer, error) {
return nil, errors.New("Container not found. ")
}
// ModifyContainerInformation Modifies information inside the container
func (TC *TrackContainer)ModifyContainerInformation() error {
// Gets all the information of tracker containers
err, t := ViewTrackedContainers()
if err != nil {
return err
}
// Find the element where the containers match and
// change them
for i, container := range t.TrackContainerList {
if TC.Id == container.Id {
t.TrackContainerList[i] = *TC
break
}
}
// Write the modified information to the file
// write modified information to the tracked json file
err = t.WriteContainers()
if err != nil {
return err
}
return nil
}
// WriteContainers Write information back to the config file
func (TC *TrackContainers)WriteContainers() error {
// Initialize config file
config,err := config.ConfigInit()
// write modified information to the tracked json file
data,err := json.MarshalIndent(TC, "", "\t")
if err != nil {
return err
}
err = ioutil.WriteFile(config.TrackContainersPath,data,0777)
if err != nil {
return err
}
return nil
}
// CheckID Checks if the ID belongs to a group or a single container
func CheckID(ID string) (string,error) {
// For group checks if the 1st characters is "grp"