import json


def writeToFiles(endpointMap, failOnError) :
    """Writes the endpoints to json files. Returns a list of all of
    the filenames written to.

    Keyword arguments:
    endpointMap - A Python dictionary mapping action name, which is used for filename,
        to Python dictionary with the Shields key value pairs.
    failOnError - Pass True to fail the workflow if an error occurs
    """
    allFilenames = []
    for actionName, endpointdata in endpointMap.items() :
        filename = actionName + ".json"
        try :
            with open(filename, "w") as jsonOut :
                json.dump(endpointdata, jsonOut, sort_keys=True)
                allFilenames.append(filename)
        except:
            print("Error: Failed while writing:", filename)
            exitCode = 2
            print("::set-output name=exit-code::" + str(exitCode))
            exit(exitCode if failOnError else 0)
    return allFilenames